How to calculate "time ago" in php

Possible Duplicate:
PHP How to find the time elapsed since a date time?

How would I get the time ago posted. Just example 3 minutes ago or more than 24 hours ago and 2 days ago out of 2012-07-13 17:55:59.


<?php
function get_timeago( $ptime )
{
    $etime = time() - $ptime;

    if( $etime < 1 )
    {
        return 'less than 1 second ago';
    }

    $a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60             =>  'hour',
                60                  =>  'minute',
                1                   =>  'second'
    );

    foreach( $a as $secs => $str )
    {
        $d = $etime / $secs;

        if( $d >= 1 )
        {
            $r = round( $d );
            return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
        }
    }
}
?>


Usage:   

   <?php 


     echo get_timeago( $timestamp );

   ?>

另外尝试strtotime()

链接地址: http://www.djcxy.com/p/58980.html

上一篇: 检查帖子“前”的时间

下一篇: 如何计算“时间前”在PHP中