将服务器时间转换为本地时间 [英] convert server time to local time

查看:58
本文介绍了将服务器时间转换为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在印度,如果我通过php date(),mysql NOW()和三列中的时间戳节省了时间,大约在2017-05-09 10:12:21在数据库中,那么它将产生结果

In india if i save time at around 2017-05-09 10:12:21 in database by php date(), mysql NOW(), and by timestamp in three column .So it produces the result

date()                      NOW()            timestamp

2017-05-09 04:38:37 |2017-05-08 22:38:37 |2017-05-08 22:38:37

2017-05-09 04:38:37 | 2017-05-08 22:38:37 | 2017-05-08 22:38:37

现在,我想根据全球用户时区将此时间转换为本地时间.我该怎么做.如果有任何错误,请原谅我是php新手.

Now I want to convert this time to local time according to worldwide users timezone . how I can do this.Please forgive for any mistake I am new in php.

推荐答案

根据

According to this, if your mysql column is of type TIMESTAMP, the date time is stored in UTC: it will be converted from local timezone while inserting and retrieving.

要检查MySQL的默认时区是什么,请尝试以下操作:

To check what the default timezone for MySQL is, try this:

SELECT @@system_time_zone;

PHP的 date 函数返回格式化的当前unix时间戳的日期字符串.它将使用当前的默认时区来格式化时间戳.您可以通过以下方法获取默认时区:

PHP's date function returns the formatted date string from the current unix timestamp. It will use the current default timezone to format the timestamp. You can get the default timezone with this:

echo date_default_timezone_get();

从您的问题示例中我了解到,这两个时区必须不同,否则它们将显示相同的时间.

From what I understand from your question's example, these two timezones must be different, or they would show the same time.

我认为将所有默认时区设置为UTC,然后根据请求的来源将其转换为本地时区是标准做法.这样做并不像看起来那样简单,因为时区不是HTTP规范的一部分,但这可以为您提供帮助:

I believe it is standard practice to set all the default timezones to UTC, and convert them to local timezone according to the request's origin. Doing this is not as straight forward as it seems, as timezones are not part of the HTTP spec, but this may help you with that: How to detect user's timezone?.

如果您只想将所有内容存储在本地时区,并且确信用户不会介意,则可以将php和mysql中的默认时区更改为本地时区.

If you just want to store everything in your local timezone and are confident the users won't mind, you can change the default timezone in php and mysql to your local timezone.

MySQL:如何设置MySQL的时区?

PHP: http://php.net/manual/en/function.date-default-timezone-set.php

这篇关于将服务器时间转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆