Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To Calculate Date Range Between Two date metafield? #8

Open
mangsinaga opened this issue Jan 3, 2024 · 2 comments
Open

How To Calculate Date Range Between Two date metafield? #8

mangsinaga opened this issue Jan 3, 2024 · 2 comments

Comments

@mangsinaga
Copy link

I didn't find a way. I created two meta fields open_date and close_date (date field with timestamp save) for an event registration created with jetengine cpt. I want to show a number like x days left / ends in x days before registration closes based on a comparison of the number of days of the two meta fields above. Can someone help me? And how does the "ending soon" text/badge appear when there is only 1 day left before the close_date?

@remygfx
Copy link

remygfx commented Apr 25, 2024

I too am interested in a way for this, in mycase I am trying to get the fields diff between a year supplied as a meta field and the current one, the crocoblock date macro %current_date|year% is ignored as an additional argument for calculation

@moxet
Copy link

moxet commented Jun 22, 2024

I used it today for calculating the expiring date of an invoice for an year, the code might be helpful.

add_filter( 'jet-engine-calculated-callback/config', function( $callbacks = array() ) {

$callbacks['invoice_expiry'] = function( $field_value ) {

	$originalDate = jet_engine()->listings->data->get_meta("due-date");
	$date = new DateTime($originalDate);

	// Add one year to the original date
	$newDate = clone $date; // Clone the original date to modify it
	$newDate->modify('+1 year');

	// Get today's date
	$today = new DateTime();

	// Calculate the difference in days between today and the new date
	$interval = $today->diff($newDate);
	$daysDifference = $interval->days;

	// Get the new date in the desired format
	$result = "<font style='color:red;'>".$daysDifference." days</font> on ".$newDate->format('F j, Y');
	return $result;

};
return $callbacks;

} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants