Skip to content

Instantly share code, notes, and snippets.

@andrewhathaway
Created November 23, 2013 12:43
Show Gist options
  • Save andrewhathaway/7614212 to your computer and use it in GitHub Desktop.
Save andrewhathaway/7614212 to your computer and use it in GitHub Desktop.
Years, Months and Days between two dates.
<?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