Created
November 23, 2013 12:43
-
-
Save andrewhathaway/7614212 to your computer and use it in GitHub Desktop.
Years, Months and Days between two dates.
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 time_elapsed($date) { | |
$date_one = new DateTime($date); | |
$date_two = new DateTime(date('jS F Y')); | |
$diff = $date_two->diff($date_one); | |
$years = plural_delegate($diff->y, 'years'); | |
$months = plural_delegate($diff->m, 'months'); | |
$days = plural_delegate($diff->d, 'days'); | |
return $years . ', ' . $months . ' and ' . $days; | |
} | |
function plural_delegate($count, $phrase) { | |
if ($count == 1) { | |
$phrase = substr($phrase, 0, -1); //Replace this crap with an Inflector | |
return $count . ' ' . $phrase; | |
} | |
return $count . ' ' . $phrase; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment