Display the relative time difference between two dates
I guess you have a bug here:


$ret = $diff-&gt;format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');



%a should be %d. With %a

time_elapsed("2012-07-08 11:14:15.889342", "2014-09-10 13:15:17.889342");


prints total number of days:

2 years, 2 months, 794 days, 2 hours, 1 minute, 02 seconds


Using %d changes 794 days to 2 days.
Another approach is iterating through of an array of units:

function time_elapsed2($start, $end) {
    $start = new DateTime($start);
    $end = new DateTime($end);

    $interval = $end-&gt;diff($start);

    $units = array(
        "%y" =&gt; sp("year", "years"),
        "%m" =&gt; sp("month", "months"),
        "%d" =&gt; sp("day", "days"),
        "%h" =&gt; sp("hour", "hours"),
        "%i" =&gt; sp("minute", "minutes"),
        "%s" =&gt; sp("second", "seconds")
    );

    $result = array();
    foreach ($units as $format_char =&gt; $names) {
        $formatted_value = $interval-&gt;format($format_char);
        if ($formatted_value == "0") {
            continue;
        }
        $result[] = get_formatted_string($formatted_value, $names);
    }  

    return implode(", ", $result);
} 

function sp($singular, $plural) {
    return array("singular" =&gt; $singular, "plural" =&gt; $plural);
}

function get_formatted_string($formatted_value, $names) {
    $result = $formatted_value . " ";
    if ($formatted_value == "1") {
        $result .= $names["singular"];
    } else {
        $result .= $names["plural"];
    }
    return $result;
}


(In production code I'd use constants instead of singular and plural.) The array also helps if you want to translate your page to other languages.