Created
April 24, 2019 17:14
-
-
Save sany2k8/c8854a417743239b1517ce4dc5890679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function pluralize( $count, $text ) | |
{ | |
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) ); | |
} | |
function finddiff($datetime1, $datetime2) | |
{ | |
$final_result = []; | |
$datetime1 = new DateTime($datetime1); | |
$datetime2 = new DateTime($datetime2); | |
$interval = $datetime1->diff($datetime2); | |
$suffix = ( $interval->invert ? ' ago' : '' ); | |
if ( $v = $interval->y >= 1 ) $final_result[] = pluralize( $interval->y, 'year' ) . $suffix; | |
if ( $v = $interval->m >= 1 ) $final_result[] = pluralize( $interval->m, 'month' ) . $suffix; | |
if ( $v = $interval->d >= 1 ) $final_result[] = pluralize( $interval->d, 'day' ) . $suffix; | |
if ( $v = $interval->h >= 1 ) $final_result[] = pluralize( $interval->h, 'hour' ) . $suffix; | |
if ( $v = $interval->i >= 1 ) $final_result[] = pluralize( $interval->i, 'minute' ) . $suffix; | |
if ( $v = $interval->s >= 1 ) $final_result[] = pluralize( $interval->s, 'second' ) . $suffix; | |
return implode(' ', $final_result); | |
} | |
$datetime1 = '2007-08-02'; | |
$datetime2 = '2009-10-13'; | |
echo finddiff($datetime1,$datetime2); | |
?> | |
Ref: https://www.php.net/manual/en/datetime.diff.php#97880 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment