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

时间:2016-02-07 11:49:54

标签: php codeigniter date

美好的一天,我有点麻烦,如果日期达到月/年,我怎么能像xx月/ s前和年/年前那样制作它。可能吗 ?无论如何,我正在制作一个消息传递模块。这是我的代码。预先感谢你帮助的人

<?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?>

1 个答案:

答案 0 :(得分:0)

您可以使用此功能将消息时间作为参数

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)秒/分钟/小时/日/月/年前”