我在datetime中创建了一个字段,在时间戳中有一个修改过的字段。 这是我的代码
date_default_timezone_set('America/Los_Angeles');
if ($this->isNewRecord)
$this->created = date("Y-m-d h:i:s");
$this->modified = date("Y-m-d h:i:s");
当我创建新记录时,我在洛杉矶时间看到同一时间的日期时间和时间戳,但我希望时间戳字段为UTC。 我的意思是如果datetime字段是'.. 08:00:00'时间戳应该是'16:00:00',因为美国时间是(UTC -08:00)而DOC说
TIMESTAMP值从当前时区转换为UTC 存储
答案 0 :(得分:4)
TIMESTAMP
fieds已转换为UTC 进行存储。这意味着,它以这种方式存储,但是当选择它时,它将被转换为连接的时区。您可以进行实验:将您的时区(见下文)更改为其他内容,然后选择该时间戳。
这是MySQL docs about handling timezones
补充:一些控制台输出用于说明
mysql> use test
Database changed
mysql> -- Let s see what is the current timezone setting on this server
mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+
1 row in set (0.00 sec)
mysql> -- I don t know what SYSTEM is set to, so just to be safe I ll set it to my timezone
mysql> set time_zone = '+01:00';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM | +01:00 |
+--------------------+---------------------+
1 row in set (0.00 sec)
mysql> -- Let s create a table for or test
mysql> create table testTimestamp (ts timestamp);
Query OK, 0 rows affected (0.28 sec)
mysql> -- Now to insert current time into the test table
mysql> insert into testTimestamp values (now());
Query OK, 1 row affected (0.01 sec)
mysql> -- Let s see how it looks like
mysql> select * from testTimestamp;
+---------------------+
| ts |
+---------------------+
| 2012-02-13 00:12:00 |
+---------------------+
1 row in set (0.03 sec)
mysql> -- Now let s change session timezone to Los Angeles time
mysql> set time_zone = '-08:00';
Query OK, 0 rows affected (0.00 sec)
mysql> -- And let s look at our timestamp once again
mysql> select * from testTimestamp;
+---------------------+
| ts |
+---------------------+
| 2012-02-12 15:12:00 |
+---------------------+
1 row in set (0.00 sec)
答案 1 :(得分:0)
您应该使用gmdate()
代替date()
date()
根据date_default_timezone_set()
返回结果,而gmdate()
根据GMT / UTC 执行相同的。
请查看The standard way for switching from/to user_timezone and gmt。