我正在使用此功能,这是在这个网站上的一些帖子中建议的(我不记得在哪里了)
function udate($format, $utimestamp = null) { if (is_null($utimestamp)) $utimestamp = microtime(true); $timestamp = floor($utimestamp); $microseconds = round(($utimestamp - $timestamp) * 1000000); return date(preg_replace('`(?<!\\\\)u`', $microseconds, $format), $timestamp); }在使用代码进行大量搜索和实验后,我想获得服务器的日期,时间和微秒,以便将其写入MySql数据库,这是唯一对我有用的解决方案。
唯一的问题是我得到UTC时间而不是服务器的本地时间,我使用timedatectl set-local-rtc 1命令将我的服务器时间timedatectl set-local-rtc 1但它没有帮助。
有没有人知道,我需要在此功能中更改什么才能让我的服务器在当地时间?
I'm using this function it was suggested in some post here on this site (i don't remember where anymore)
function udate($format, $utimestamp = null) { if (is_null($utimestamp)) $utimestamp = microtime(true); $timestamp = floor($utimestamp); $microseconds = round(($utimestamp - $timestamp) * 1000000); return date(preg_replace('`(?<!\\\\)u`', $microseconds, $format), $timestamp); }After searching and experimenting a lot with the code, I wanted to get the date,time and the microseconds of the server so I can write it into my MySql database and this was the only solution that worked for me.
The only Problem is that I'm getting the UTC time and not the local time of the server, I've set my server time to local using the timedatectl set-local-rtc 1 command but it didn't help.
Does any one have an idea, what do I have to change in this function, to get my servers local time?
最满意答案
你看过DateTime和DateTimeZone类了吗?
$date = new \DateTime('now', new \DateTimeZone('[SOURCE TZ]')); $date->setTimezone(new \DateTimeZone('[DEST TZ]')); return $date->format('Y-m-d h:i:s.u');您可以如上设置源和目标时区。 您可以完全没有源时区。
要将此应用于您的代码,您可能需要执行类似的操作
\DateTime::createFromFormat('U.u', microtime(true))Have you had a look at the DateTime and DateTimeZone classes?
$date = new \DateTime('now', new \DateTimeZone('[SOURCE TZ]')); $date->setTimezone(new \DateTimeZone('[DEST TZ]')); return $date->format('Y-m-d h:i:s.u');You can set source and destination timezones as above. You may be able to do without the source time zone entirely.
To apply this to your code, you will probably need to do something like
\DateTime::createFromFormat('U.u', microtime(true))更多推荐
发布评论