Seconds / minutes/ hours ago

Hello.

In reply to a seconds / hours/ days ago script , Iwould like to ask something for. That’s look pretty easy , but I really do not know what to do.

function time_elapsed($ts)
{
    $time_periods = array_fill_keys(array('hour', 'minute', 'second'), 0);
    $now = time();
    if ($ts > $now) {
        list($now, $ts) = array($ts, $now);
    }
    foreach ($time_periods as $period => $count) {
        while (($tmp = strtotime('+1 ' . $period, $ts)) < $now) {
            $ts = $tmp;
            $time_periods[$period]++;
        }
    }
    return $time_periods;
} 

execution :

foreach ($posted_times as $time) {
    $timestamp = strtotime($time);
    $time_arr = time_elapsed($timestamp);
    $str = '';
    foreach ($time_arr as $key => $val) {
        if ($val) {
            $str .= $val . ' ' . $key;
            $str .= $val > 1 ? 's, ' : ', ';
        }
    }
    $direction = $timestamp < time() ? ' ago' : ' from now';
    echo $time . ' -- ' . rtrim($str, ', ') . ' '
       . $direction . '.<br />';
} 

What should I do to change a indexes from ( ‘hour’, ‘minute’, ‘second’) to ( ‘h’ , ‘m’ , ‘s’ ) ?

Because normally change in this part :

 $time_periods = array_fill_keys(array('hour', 'minute', 'second'), 0);

do not work.

Solved.

When you solve something, you should tell or show what you did so others can benefit from it.