0

I'm trying to calculate the difference in days between two post custom field values that have been inputted via a front end date picker (gravity forms).

A simplified version of what I have to get my two dates is below:

 <?php $date1 = get_field('date_start', false, false);
 $date1 = new DateTime($date1); ?>

 <p><?php echo $date1->format('j M'); ?></p>



 <?php $date2 = get_field('date_fin', false, false);
 $date2 = new DateTime($date2); ?>

 <p><?php echo $date2->format('j M'); ?></p>

I need to calculate and output the day difference between the above outputted dates.

Say if I had an output like:

3 jun

10 jun

I would like to be able to create a variable or some type of php output that would say:

'7 days late'

Then to make things even more complicated if the difference is zero I would like to say:

'On time'

  • Format them as Unix epoch timestamps instead of ('j M'). Then, you can do a comparison like if($date2 > $date1) { echo $date2-$date1 . ' days late'; } elseif($date2 == $date1) { echo 'on time'; } else { echo $date1-$date2 . ' days early'; } – WebElaine Jun 12 '17 at 18:27
1

We can use something like human_time_diff() to give us a readable time difference. First we need to see if the days are the same:

$date1 = get_field( 'date_start', false, false );
$date1 = new DateTime( $date1 );
$unix1 = strtotime( $date1->format( 'Y-m-d' ) );

$date2 = get_field( 'date_fin', false, false );
$date2 = new DateTime( $date2 )
$unix2 = strtotime( $date2->format( 'Y-m-d' ) );

if( 0 === ( $unix1 - $unix2 ) ) {
    echo 'On Time!';
} elseif( $unix2 < $unix1 ) {
    echo human_time_diff( $unix1, $unix2 ) . ' Early';
} else {
    echo human_time_diff( $unix1, $unix2 ) . ' Late';
}

Some example with output:

Date 1 = June 3rd
Date 2 = June 3rd
Output: On Time!

Date 1 = June 10th
Date 2 = June 13th
Output: 3 Days Late

Date 1 = June 10th
Date 2 = June 3rd
Output: 1 Week Early


If we wanted to show the actual days instead of week or month as human_time_diff() does we can just subtract the largest from the smallest, divide by the WordPress constant variable DAY_IN_SECONDS. We'll then run it through the _n() function to display plural or singular.

if( 0 === ( $unix1 - $unix2 ) ) {
    echo 'On Time!';
} elseif( $unix2 < $unix1 ) {
    $days = ( intval( $unix1 - $unix2 ) / DAY_IN_SECONDS );
    printf( _n( '%s day early', '%s days early', $days ), $days );
} else {
    $days = ( intval( $unix2 - $unix1 ) / DAY_IN_SECONDS );
    printf( _n( '%s day late', '%s days late', $days ), $days );
}
|improve this answer|||||
  • Great thanks so much for this. Will give it a try when I'm back in the office. Quick question - if I always wanted it to be actual day count how would that be done? So instead of the 3rd output where it says '1 week early' it would say '7 days early'. – joelybristol Jun 12 '17 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.