如何在4分钟前在php

时间:2016-04-14 11:00:48

标签: php mysql date time

我想在4分钟前或1天前在我的网络应用程序中获取msg我在mysql和TIMESTAMP类型的字段中创建了一个表并且默认2016-04-14 16:02:00但是这个函数正在返回3小时前,应该是10分钟前 我尝试设置时区和回声时间似乎完全没问题,但返回值有3个小时前

function timeAgo($timestamp){
    $datetime1=new DateTime("now");


    $datetime1->setTimezone(new DateTimeZone('Asia/Calcutta')); 

    echo $datetime1->format('Y-m-d H:i:s');
    echo "<br>";

    $datetime2=date_create($timestamp);
    echo $datetime2->format('Y-m-d H:i:s');
    echo "<br>";                            

    $diff=date_diff($datetime1, $datetime2);                            

    $timemsg='';
    if($diff->y > 0){
        $timemsg = $diff->y .' year'. ($diff->y > 1?"'s":'');

    }
    else if($diff->m > 0){
     $timemsg = $diff->m . ' month'. ($diff->m > 1?"'s":'');
    }
    else if($diff->d > 0){
     $timemsg = $diff->d .' day'. ($diff->d > 1?"'s":'');
    }
    else if($diff->h > 0){
     $timemsg = $diff->h .' hour'.($diff->h > 1 ? "'s":'');
    }
    else if($diff->i > 0){
     $timemsg = $diff->i .' minute'. ($diff->i > 1?"'s":'');
    }
    else if($diff->s > 0){
     $timemsg = $diff->s .' second'. ($diff->s > 1?"'s":'');
    }
    $timemsg = $timemsg.' ago';
    return $timemsg;
}

这就是

2016-04-14 16:21:02
2016-04-14 16:02:00
3 hour's ago

代码说3个小时哪个不对,出错了什么?

0 个答案:

没有答案