Handling time zone issues in PHP is a critical task, especially when your application is targeted to users around the world, or is deployed on servers with multiple time zones. date_default_timezone_set() and date_timezone_get() are two important functions used in PHP for time zone management. They each play different roles. Reasonable use can ensure the accuracy of time processing.
The function of this function is to set a default time zone for the current script. All times generated using date() , time() and other functions will be calculated based on this time zone.
<?php
// Set the default time zone to Tokyo
date_default_timezone_set('Asia/Tokyo');
echo date('Y-m-d H:i:s');
// Output:2025-05-26 16:45:30(Assume Tokyo time)
?>
In actual development, the default time zone can be set uniformly in the application's initialization file to ensure that the time behavior of the entire application is consistent.
Unlike the global default settings above, date_timezone_get() is mainly used to extract the current set time zone information from the DateTime object. This function is more suitable for local time management, such as if you want to handle time according to user preferences.
<?php
$date = new DateTime('now', new DateTimeZone('Europe/Paris'));
$timezone = date_timezone_get($date);
echo $timezone->getName();
// Output:Europe/Paris
?>
This function returns a DateTimeZone object, which can continue to call its methods to get more information about the time zone, such as offsets.
In practical applications, you may need to convert time according to different time zones where the server and the user are located. For example, if the user selects a time zone in his personal settings, you need to display the time in the user time zone, while the database records adopt the server's default time zone.
<?php
// Server default time zone(UTC)
date_default_timezone_set('UTC');
// Get it from the database UTC time
$utcTime = '2025-05-26 08:00:00';
$date = new DateTime($utcTime, new DateTimeZone('UTC'));
// User preference time zone
$userTimezone = new DateTimeZone('America/New_York');
$date->setTimezone($userTimezone);
echo '用户本地time:' . $date->format('Y-m-d H:i:s');
// Output:用户本地time:2025-05-26 04:00:00
?>
This code clearly shows how to set the basic environment with date_default_timezone_set() , and then implement object-level time zone management through DateTime and date_timezone_get() .
Sometimes you also need to confirm the default time zone of the current script running, which can be obtained through date_default_timezone_get() :
<?php
echo date_default_timezone_get();
// Output:UTC(Assume that the previous setting is UTC)
?>
This is especially useful for features such as debugging and log output, ensuring that time zone settings are as expected.
When building applications that support multi-time zones, it is recommended:
UTC is used uniformly for all database time records.
When receiving user input time, it is converted to UTC storage, and when displayed, it is converted to user time zone.
Use DateTime and DateTimeZone objects instead of simple string operations.
In addition, it can also be combined with external services or configurations to allow users to freely select time zones and persist their settings.