消息日期前(xx 个月前或几年前)


message date ago (xx month ago or years)

伙计们,我遇到了一些麻烦,如果天数达到月/年数,我怎么能像 xx 个月前和一年前一样。可能吗?无论如何,我正在制作一个消息传递模块。这是我的代码。提前感谢您的帮助

<?php       
   date_default_timezone_set('Asia/Manila');
    $now = strtotime(date("Y-m-d H:i:s"));
    $date = strtotime($key->message_date); //this is the date where message was sent
    $dateDiff = abs($now - $date);
    $fullDays = floor($dateDiff/(60*60*24));
     if($fullDays==0)
     {
        echo " Today ";
     }
     else if($fullDays==1)
     {
        echo " Yesterday ";
     }
     else
     {
        echo $fullDays ." days ago";
     }
     $at=date('g:iA',$date)
    ?> at <?php echo $at?>

您可以使用此函数 传递消息时间作为参数

function convert_time($ptime){
        $etime = time() - $ptime;
        if($etime < 1){
            return '0 seconds';
        }
        $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 $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
            }
        }
    }

在此函数中,您可以获得"(1)秒/分钟/小时/天/月/年前"