简体   繁体   English

使用UTC时间以小时前,几天前等返回mysql datetime?

[英]returning mysql datetime as hours ago, days ago etc using UTC time?

i am using the following script which converts my standard DATETIME stamp to display as hours ago, days ago etc. 我正在使用以下脚本将我的标准DATETIME标记转换为小时前,几天前等。

    <?php require_once 'config.php'; ?>

<?php
date_default_timezone_set('Europe/Kaliningrad');
function pretty_relative_time($time) {
 if ($time !== intval($time)) { $time = strtotime($time); }
 $d = time() - $time;
 if ($time < strtotime(date('Y-m-d 00:00:00')) - 60*60*24*3) {
 $format = 'F j, Y';
 if (date('Y') !== date('Y', $time)) {
$format .= ", Y";
 }
 return date($format, $time);
 }
 if ($d >= 60*60*24) {
 $day = 'Yesterday';
 if (date('l', time() - 60*60*24) !== date('l', $time)) { $day = date('l', $time); }
 return $day . " at " . date('g:ia', $time);
 }
 if ($d >= 60*60*2) { return intval($d / (60*60)) . " hours ago"; }
 if ($d >= 60*60) { return "less than an hour ago"; }
 if ($d >= 60*2) { return intval($d / 60) . " minutes ago"; }
 if ($d >= 60) { return "about a minute ago"; }
 if ($d >= 2) { return intval($d) . " seconds ago"; }
 return "a few seconds ago";
}
?>

I then call the function like so: 然后我调用这个函数:

echo pretty_relative_time($row['user_blocked_timestamp']);

this works fine on local host however when tested on my server i get the following error: 这在本地主机上工作正常,但在我的服务器上测试时,我收到以下错误:

It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone set() function.

Can someone please explain why i am getting this error and what i can do to fix it. 有人可以解释为什么我收到此错误以及我可以做些什么来解决它。

so far i am storing all my DATETIME stamps using: 到目前为止,我正在使用以下方式存储我的所有DATETIME标记:

UTC_TIMESTAMP() rather than now()

thanks in advance. 提前致谢。

Just do what the error tells you, on the top of the page put the default timezone. 只需按照错误告诉你的内容,在页面顶部放置默认时区。

date_default_timezone_set('Europe/Kaliningrad');

You can find all the timezone names here. 您可以在此处找到所有时区名称。 http://php.net/manual/en/timezones.php http://php.net/manual/en/timezones.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM