Skip Navigation

[Resolved] Display different between dates

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to display the difference between two dates, One being Today and the other being a date field from the post.

Solution:

Naturally you're not able to do this with our toolset plugins however with some custom code you can achieve this.

Add the following to your custom functions in Toolset -> Settings -> Custom Code


function calculate_date_diff( $atts ) {
    
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
        ),
        $atts
    );
    
    $field = get_post_meta( $atts['post_id'], 'wpcf-last-date-of-application', true );
      
     $userTimezone = new DateTimeZone('Europe/Rome');
     $date = new DateTime('now',$userTimezone);
     $today_date =  strtotime($date->format('Y/m/d H:i'));
     $text = ' day left';
     if(((($field - $today_date)/60/60/24)+1)>1){
$text = 'days left';
}
     return ((($field - $today_date)/60/60/24)+1).' '.$text;  
}
add_shortcode( 'calculate_date_diff', 'calculate_date_diff' );

To use this shortcode just replace the "wpcf-last-date-of-application" with the slug of your custom date field keeping the wpcf- prefix.

Also you will need to set your timezone with the line
$userTimezone = new DateTimeZone('Europe/Rome');

A list of timezones can be seen in the link below.
https://www.php.net/manual/en/timezones.php

This will ensure that the correct information for Today() is set.

Finally the shortcode usage is [calculate_date_diff post_id='[wpv-post-id]'] where you will provide the ID of the post to get the date field.

100% of people find this useful.

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 11 replies, has 2 voices.

Last updated by deepS 5 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1338039

Hi, I have a job website with post type called 'Jobs' and it contains a custom field named 'Last date of application'. Now I want to display the difference between the 'Last date of application' and 'Today' i.e. days to expiry of the job.
I found one solution like https://toolset.com/forums/topic/display-difference-between-2-dates-fields/ but cannot utilize in my case.
Please help.

#1338953

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

Thank you for getting in touch.

The custom field "Last date of application" is a custom Date field correct?

Could you try this custom shortcode below?


// Add Shortcode
function calculate_date_diff( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'post_id' => '',
		),
		$atts
	);

	$field = get_post_meta( $atts['post_id'], 'wpcf-date-field', true );
	$date = date('Y-m-d');
	
	$today_date = strtotime($date);
	
	return round($today_date - $field).' ago';

}
add_shortcode( 'calculate_date_diff', 'calculate_date_diff' );

So you would add this to your Toolset custom code section in Toolset -> Settings -> Custom Code.

The usage of the shortcode is.
[calculate_date_diff post_id='[wpv-post-id]']

NOTE: you need to replace the "wpcf-date-field" with the slug of your date field keeping the wpcf- prefix attached to the slug.

Please let me know if this helps.
Thanks,
Shane

#1339345
Screenshot_2.jpg
Screenshot_1.jpg

Hi, I think there is some issue with the code it's displaying some abrupt figures or maybe I couldn't utilize it properly.
I have used it in the 'Homepage' view (screenshot_2). You can see the front-end hidden link (screenshot_1). Please see the issue.
I am ready to provide you access to our site back-end. Thanks.
P.S.: Since this is a production site I am removing the code till it has been rectified.

#1339617

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

Yes please provide admin access to the site so that i can have a look at this for you.

Thanks,
Shane

#1339891

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

I've corrected the shortcode now and it should work.

Here is the correct shortcode below.



function calculate_date_diff( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
        ),
        $atts
    );
 
    $field = get_post_meta( $atts['post_id'], 'wpcf-last-date-of-application', true );
    $date = date('Y-m-d');
     
    $today_date = strtotime($date);
    return (($field - $today_date)/60/60/24).' Days Left';
 
}
add_shortcode( 'calculate_date_diff', 'calculate_date_diff' );

Please let me know if this helps.
Thanks,
Shane

#1340053
Screenshot_3.jpg

Hi, your code works nicely. I made a simple change by adding '+1' in line no. 24 i.e.

return ((($field - $today_date)/60/60/24)+1).' days left';

to simply display 1 day addition to the difference between the dates. Hope the addition is ok.
Please do me a favor by making small changes into the code to display 'Just 1 day left' where the difference between the dates is just 1. And to display 'days left' where the difference between the dates is more than 1.
Hope you got my point.
Thank you.

#1340135
Screenshot_4.jpg

Hi,
Another query related to the same issue. I have just noticed. I don't know whether it's a problem with the timezone or not. I am writing this when the local time is 00.20 hours and the date is September 14, 2019. So the difference between the dates should be literally 0 and since I have added 1 it should come to '1 days left'. But, in the fron end, it's showing '2 days left'. Please see the screenshot. There are three markings that can explain you things.
Hope I made you understand.
Thank you.

#1340155

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

So the issue is that the date for today is getting the time based on your server time.

We need to make another change to the code so you can set the timezone of the "Today".


function calculate_date_diff( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
        ),
        $atts
    );
  
    $field = get_post_meta( $atts['post_id'], 'wpcf-last-date-of-application', true );
    
     $userTimezone = new DateTimeZone('Europe/Rome');
     $date = new DateTime('now',$userTimezone);
     $today_date =  strtotime($date->format('Y/m/d H:i'));

     return ((($field - $today_date)/60/60/24)+1).' days left';  
}
add_shortcode( 'calculate_date_diff', 'calculate_date_diff' );


Not you need to set the timezone that you would like to use here
$userTimezone = new DateTimeZone('Europe/Rome');

For a list of timezones please take a look at the link below.
hidden link

Thanks,
Shane

#1340169
Screenshot_5.jpg

Hey, my date issue has been resolved. Thank you very much. But as I have asked you earlier to make necessary changes into the code to display 'Just 1 day left' where the difference between the dates is just 1. And to display 'days left' where the difference between the dates is more than 1. Hope you are getting my point.

#1340179

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Deep,

I understand your point now.

This will require some conditional to achieve.

function calculate_date_diff( $atts ) {
   
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
        ),
        $atts
    );
   
    $field = get_post_meta( $atts['post_id'], 'wpcf-last-date-of-application', true );
     
     $userTimezone = new DateTimeZone('Europe/Rome');
     $date = new DateTime('now',$userTimezone);
     $today_date =  strtotime($date->format('Y/m/d H:i'));
     $text = ' day left';
     if(((($field - $today_date)/60/60/24)+1)>1){
$text = 'days left';
}
     return ((($field - $today_date)/60/60/24)+1).' '.$text;  
}
add_shortcode( 'calculate_date_diff', 'calculate_date_diff' );

Please try this now.
Thanks,
Shane

#1340191
Screenshot_6.jpg

It works, but see I wanted to show it like 'Just 1 day left', I mean putting the calculated figure between two strings. Hence I made certain changes to the code but it went wrong somewhere. Please help me with the code:

function calculate_date_diff( $atts ) {
    
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
        ),
        $atts
    );
    
    $field = get_post_meta( $atts['post_id'], 'wpcf-last-date-of-application', true );
      
     $userTimezone = new DateTimeZone('Asia/Kolkata');
     $date = new DateTime('now',$userTimezone);
     $today_date =  strtotime($date->format('Y/m/d'));
     $text = ' days left';
     if(((($field - $today_date)/60/60/24)+1)>1){
       return ((($field - $today_date)/60/60/24)+1).' '.$text;
     } else {
         echo "Just 1 day left";
     }
}
add_shortcode( 'calculate_date_diff', 'calculate_date_diff' );

Where the difference between the dates is 1, it draws blank.

#1340211

My issue is resolved now. Thank you!