Hướng dẫn dùng strptime timezone trong PHP
Đôi lúc các bạn xem các hướng dẫn về hàm lấy thời gian :date(format,timestamp). Nếu chưa biết hàm date(format,timestamp) bạn có thể xem ở link:http://congnghe5s.com/bai-viet/xu-ly-ngay-gio-trong-php.html. Bạn thực hiện test lệnh trên máy và kết quả không khớp với time hiện tại. Đó là vì lí do thời gian hay timezone trên server bạn đã cài đặt khác vì thế để trả về đúng time bạn muốn bạn có thể dùng hàm: Cú pháp:date_default_timezone_set ( string tham số : $
date_default_timezone_set('Asia/Ho_Chi_Minh'). Sau đó các bạn dùng hàm : echo date('d/m/Y - H:i:s'); ra đúng kết quả time ở Việt Nam. Ví dụ: date_default_timezone_set('Asia/Ho_Chi_Minh'); if (date_default_timezone_get()) { echo 'date_default_timezone_set: ' . date_default_timezone_get() . ' '; } echo date('d/m/Y H:i:s'); Ví dụ trên có 1 hàm cũng khá quan trọng : date_default_timezone_get() trả về timezone mặc định của server bạn đang chạy hoặc là đã được thiết lập thông qua date_default_timezone_set(). Qua bài viết hy vọng các bạn có thêm kiến thức khi xử lý ngày tháng trong PHP. Đôi lúc các bạn xem các hướng dẫn về hàm lấy thời gian :date(format,timestamp). Nếu chưa biết hàm date(format,timestamp) bạn có thể xem ở link:http://congnghe5s.com/bai-viet/xu-ly-ngay-gio-trong-php.html. Bạn thực hiện test lệnh trên máy và kết quả không khớp với time hiện tại. Đó là vì lí do thời gian hay timezone trên server bạn đã cài đặt khác vì thế để trả về đúng time bạn muốn bạn có thể dùng hàm: Cú pháp:date_default_timezone_set ( string tham số : $
date_default_timezone_set('Asia/Ho_Chi_Minh'). Sau đó các bạn dùng hàm : echo date('d/m/Y - H:i:s'); ra đúng kết quả time ở Việt Nam. Ví dụ: date_default_timezone_set('Asia/Ho_Chi_Minh'); if (date_default_timezone_get()) { echo 'date_default_timezone_set: ' . date_default_timezone_get() . ' '; } echo date('d/m/Y H:i:s'); Ví dụ trên có 1 hàm cũng khá quan trọng : date_default_timezone_get() trả về timezone mặc định của server bạn đang chạy hoặc là đã được thiết lập thông qua date_default_timezone_set(). Qua bài viết hy vọng các bạn có thêm kiến thức khi xử lý ngày tháng trong PHP. 5.1.15 MySQL Server Time Zone SupportThis section describes the time zone settings maintained by MySQL, how to load the system tables required for named time support, how to stay current with time zone changes, and how to enable leap-second support. Beginning with MySQL 8.0.19, time zone offsets are also supported for inserted datetime values; see Section 11.2.2, “The DATE, DATETIME, and TIMESTAMP Types”, for more information. For information about time zone settings in replication setups, see Section 17.5.1.14, “Replication and System Functions” and Section 17.5.1.33, “Replication and Time Zones”.
Time Zone VariablesMySQL Server maintains several time zone settings:
The session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as The session time zone setting does not affect values displayed by functions such as The current global and session time zone values can be retrieved like this:
Populating the Time Zone Tables Several tables in the Note Loading the time zone information is not necessarily a one-time operation because the information changes occasionally. When such changes occur, applications that use the old rules become out of date and you may find it necessary to reload the time zone tables to keep the information used by your MySQL server current. See Staying Current with Time Zone Changes. If your system has its own zoneinfo database (the set of files describing time zones), use the mysql_tzinfo_to_sql program to load the time zone tables. Examples of such systems are Linux, macOS, FreeBSD, and Solaris. One likely location for these files is the To load the time zone tables from the command line, pass the zoneinfo directory path name to mysql_tzinfo_to_sql and send the output into the mysql program. For example:
The mysql command shown here assumes that you connect to the server using an account such as mysql_tzinfo_to_sql reads your system's time zone files and generates SQL statements from them. mysql processes those statements to load the time zone tables. mysql_tzinfo_to_sql also can be used to load a single time zone file or generate leap second information:
After running mysql_tzinfo_to_sql, restart the server so that it does not continue to use any previously cached time zone data. If your system has no zoneinfo database (for example, Windows), you can use a package containing SQL statements that is available for download at the MySQL Developer Zone:
Warning Do not use a downloadable time zone package if your system has a zoneinfo database. Use the mysql_tzinfo_to_sql utility instead. Otherwise, you may cause a difference in datetime handling between MySQL and other applications on your system. To use an SQL-statement time zone package that you have downloaded, unpack it, then load the unpacked file contents into the time zone tables:
Then restart the server. Warning Do not use a downloadable time zone package that contains Staying Current with Time Zone ChangesWhen time zone rules change, applications that use the old rules become out of date. To stay current, it is necessary to make sure that your system uses current time zone information is used. For MySQL, there are multiple factors to consider in staying current:
If you are uncertain whether named time zones are available, for use either as the server's time zone setting or by clients that set their own time zone, check whether your time zone tables are empty. The following query determines whether the table that contains time zone names has any rows:
A count of zero indicates that the table is empty. In this case, no applications currently are using named time zones, and you need not update the tables (unless you want to enable named time zone support). A count greater than zero indicates that the table is not empty and that its contents are available to be used for named time zone support. In this case, be sure to reload your time zone tables so that applications that use named time zones can obtain correct query results. To check whether your MySQL installation is updated properly for a change in Daylight Saving Time rules, use a test like the one following. The example uses values that are appropriate for the 2007 DST 1-hour change that occurs in the United States on March 11 at 2 a.m. The test uses this query:
The two time values indicate the times at which the DST change occurs, and the use of named time zones requires that the time zone tables be used. The desired result is that both queries return the same result (the input time, converted to the equivalent value in the 'US/Central' time zone). Before updating the time zone tables, you see an incorrect result like this:
After updating the tables, you should see the correct result:
Time Zone Leap Second Support Leap second values are returned with a time part that ends with If it is necessary to search for
To work around this, you can use a comparison based on the UTC value actually stored in the column, which has the leap second correction applied:
|