The database uses the datetime field type to record time. How to set the server time zone, mysql time zone, and PHP time zone?
Option 1.
All servers use the UTC time zone uniformly. The PHP program uses the UTC time zone. The datetime type time written to the database is UTC, and it is taken out and converted to the local time zone.
Option 2.
The time zone of all servers can be set arbitrarily, and the program time zone can be set arbitrarily. However, all those involving time storage use the int type to store timestamps.
我想大声告诉你2017-05-16 13:10:23
Why don’t you set your database to Asia/Shanghai
this time zone for PHP?
迷茫2017-05-16 13:10:23
The time zone of the server must be unified. Usually I record timestamps in the database. Because I find it convenient to search. For cross-border visits. Just perform the corresponding processing when formatting the timestamp.
大家讲道理2017-05-16 13:10:23
Finally I understand why time must be timestamped. It turns out it is because of time zone issues. However, the carbon class in Laravel seems to have solved this problem. You can convert the time format at will. Of course, timestamp performance is the most powerful, especially when searching, the int type is much more efficient than datetime.
伊谢尔伦2017-05-16 13:10:23
Strictly speaking, no matter where you are on the earth, the timestamp at any point in time is the same. For example, the timestamp 1452783600 is 4 pm in Paris and 7 am in San Francisco. This is helpful for online and client-side distributed applications to uniformly track time information.
天蓬老师2017-05-16 13:10:23
+8 hours. In addition, it is best to save all time formats with timestamps. Timestamps will not have time zone problems. Timestamps have no concept of time zones.
Strictly speaking, no matter where you are on the earth, the timestamp at any point in time is the same.
PHP中文网2017-05-16 13:10:23
1. Use datetime
,php/mysql时区设置为UTC+08
2.使用int,直接存时间戳,显示的时候再使用date
to format it