0

Possible Duplicate:
PHP: producing relative date/time from timestamps

i search so much on internet but i didn't find how can i create time counter by using php.Exactly i want to this if i post article so i want to show users how much time before this article is posted on website .

firstly it show time in SECONDS, example : 50 sec ago.

secondly after 60 seconds it shows time in MINUTES, example : 1 min ago.

thirdly after 1 hour it will show time in HOURS, example : 2 hours ago.

fourthly after 24 hour it will show time in DAYS, example : 4 days ago.

i also find this thing by jquery but i want to do this in php with Ajax. if i find how can i do this with php after that easily i can implement this with Ajax.

4

1 回答 1

1
function getTimeAgoStr($time) {
    $since = abs(time() - $time);

    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
        array(1 , 'second')
    );

    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    return $print;
}
于 2013-01-23T09:13:05.213 回答