获取服务器的本地日期,时间和微秒(Getting the local date, time and microseconds of the server)

编程入门 行业动态 更新时间:2024-10-26 12:28:16
获取服务器的本地日期,时间和微秒(Getting the local date, time and microseconds of the server) mysql

我正在使用此功能,这是在这个网站上的一些帖子中建议的(我不记得在哪里了)

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

更多推荐

本文发布于:2023-08-07 01:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:微秒   日期   服务器   时间   local

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!