sql时间戳不能显示正确的时间

h7wcgrx3  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(308)

我在我的网站上做了一个帖子,想显示时间stemp为“3分钟前”,但这是我的功能,这是工作不正常。
mysql数据库 timestamp 更新后传递为 $time 就像 2018-04-15 09:00:02 这篇文章的制作时间仅仅是几分钟前的事,但在功能完成后的页面上的结果显示了这一点 9 Hours ago 请帮我显示正确的时间

public static function timeago($time){
   $time=strtotime($time);
   $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
   $lengths = array("60","60","24","7","4.35","12","10");
   $now = time();
     $difference = $now - $time;
     $tense= "ago";
     for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
       $difference /= $lengths[$j];
     }
     $difference = round($difference);
     if($difference != 1) {
       $periods[$j].= "s";
     }
   return "$difference $periods[$j] $tense ";
}
t3psigkw

t3psigkw1#

你的问题是你没有为时间戳值写一个语句,我的意思是你在函数中使用的每个值都被认为是时间戳而不是时间字符串。

public static function timeago($time){
    if (!is_numeric($time) || (int)$time != $time)
        $time=strtotime($time);
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");
    $now = time();
    $difference = $now - $time;
    $tense= "ago";
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if($difference != 1) {
        $periods[$j].= "s";
    }
   return "$difference $periods[$j] $tense ";
}

顺便说一句,我很抱歉我的英语,这不是我的母语。

相关问题