php MySQL now() 更改时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10720887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL now() change timezone
提问by Harinder
I'm using the following INSERT statement:
我正在使用以下 INSERT 语句:
INSERT INTO messages SET `to` = '".$to."', `from` = '".$this->userid."', `title` = '".$title."', `message` = '".$message."', `created` = NOW()
However, it uses my server time (America/Montreal). I want time zone of Asia (Asia/Calcutta)
但是,它使用我的服务器时间(美国/蒙特利尔)。我想要亚洲的时区(亚洲/加尔各答)
Is this possible with the same query?
这可能与相同的查询吗?
采纳答案by wesside
You would want to go ahead and use the CONVERT_TZ()function in MySQL. It's based off the Olson databasewhich your operating system uses.
您可能想要继续使用CONVERT_TZ()MySQL 中的函数。它基于您的操作系统使用的Olson 数据库。
Hereis the documentation.
这是文档。
回答by Robbie
After you open the connection to MySQL, run the following as a query:
打开与 MySQL 的连接后,运行以下命令作为查询:
SET time_zone = timezone;
Then all functions you do will run for that timezone for that connection (i.e. until you close the "link" to the database".
然后,您执行的所有功能都将针对该连接在该时区运行(即,直到您关闭到数据库的“链接”为止)。
If you have the appropriate permissions you can lock it "permanently" / globaly. Tiemzone strings are standard strings as you have in your question.
如果您有适当的权限,您可以“永久”/全局锁定它。Tiemzone 字符串是您在问题中所使用的标准字符串。
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
回答by Vlado
Better use the SQL format directly in your query:
最好在查询中直接使用 SQL 格式:
..`created` = CONVERT_TZ(NOW(),'SYSTEM','Asia/Calcutta')..
回答by Vishal
$myDateTime = new DateTime('2012-05-23 17:01', new DateTimeZone('GMT'));
$myDateTime->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $myDateTime->format('Y-m-d H:i');
After modification to above code, such as desired format; you could use $myDateTimevariable to insert into database.
修改以上代码后,如需要的格式;您可以使用$myDateTime变量插入数据库。

