@ 2015-11-21T04:01:54Z <?php
//helper function to pluralize words
function pluralize( $count, $text ){
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
/**
* Difference of dates
* @param type $date1 DateTime first date
* @param type $date2 DateTime second date
* @return type string year, month, day, hour, minute, second
*/
function timeDiff($date1, $date2 ){
$string = "";
$interval =$date1->diff($date2);
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) $string .= pluralize( $interval->y, 'year' ). ', ';
if ( $v = $interval->m >= 1 ) $string .= pluralize( $interval->m, 'month' ). ', ';
if ( $v = $interval->d >= 1 ) $string .= pluralize( $interval->d, 'day' ). ', ';
if ( $v = $interval->h >= 1 ) $string .= pluralize( ($interval->h), 'hour' ) . ', ';
if ( $v = $interval->i >= 1 ) $string .= pluralize( $interval->i, 'minute' ). ', ';
if ( $v = $interval->i >= 1 ) $string .= pluralize( $interval->s, 'second' ). ' ';
return $string . $suffix;
}
$date1 = new DateTime("2014-05-10 20:00:00");
$date2 = new DateTime("2015-11-20 19:45:00");
//This should produce "1 year, 6 months, 9 days, 23 hours, 45 minutes"
//But it does produce "1 year, 6 months, 10 days, 45 minutes"
echo timeDiff($date1, $date2);
$date3 = new DateTime("2015-11-20 20:00:00");
echo "\n";
//This should, and does produce "1 year, 6 months, 10 days"
echo timeDiff($date1, $date3);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.2.0 - 8.2.19 , 8.3.0 - 8.3.7 Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /in/Kp26c on line 5
1 year, 6 months, 9 days, 23 hours, 45 minutes, 0 seconds
1 year, 6 months, 10 days, Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.23 , 5.5.0 - 5.5.7 , 7.3.32 - 7.3.33 , 8.0.13 , 8.1.0 - 8.1.28 1 year, 6 months, 9 days, 23 hours, 45 minutes, 0 seconds
1 year, 6 months, 10 days, Output for 5.4.24 - 5.4.45 , 5.5.8 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.31 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.12 , 8.0.14 - 8.0.30 1 year, 6 months, 10 days, 45 minutes, 0 seconds
1 year, 6 months, 10 days, Output for 5.2.0 - 5.2.17 Fatal error: Call to undefined method DateTime::diff() in /in/Kp26c on line 16
Process exited with code 255 . Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 Fatal error: Class 'DateTime' not found in /in/Kp26c on line 28
Process exited with code 255 . Output for 4.3.2 - 4.3.11 , 4.4.0 - 4.4.9 Fatal error: Cannot instantiate non-existent class: datetime in /in/Kp26c on line 28
Process exited with code 255 . Output for 4.3.0 - 4.3.1 Fatal error: Cannot instantiate non-existent class: datetime in /in/Kp26c on line 28
preferences:dark mode live preview
169.67 ms | 401 KiB | 460 Q