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.
Why don’t you set your database to
Asia/Shanghai
this time zone for PHP?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.
Only timestamp is recommended
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.
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.
+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.
1. Use
datetime
,php/mysql时区设置为UTC+08
2.使用int,直接存时间戳,显示的时候再使用
date
to format it