date_default_timezone_set()
Learn how to use PHP's date_default_timezone_set() function to control the timezone for every date/time function in a script, with examples and gotchas.
Introduction
The date_default_timezone_set() function sets the default timezone used by every date/time function in a PHP script. Once you call it, functions like date(), mktime(), strtotime(), and DateTime all interpret and format times relative to that timezone. This page explains what the function does, when you need it, and the pitfalls to watch for.
Syntax
date_default_timezone_set(string $timezoneId): bool$timezoneId is a timezone identifier string from the IANA database, such as Europe/London or Asia/Tokyo — not an abbreviation like EST. The function returns true on success and false if the identifier is invalid. When the identifier is invalid, PHP also emits an E_WARNING and falls back to the previously configured timezone.
Why it matters
A timestamp is just a number of seconds since the Unix epoch (UTC). To turn that number into a human-readable date — "2024-03-15 14:30" — PHP needs to know which timezone to render it in. If you don't set one, PHP uses the date.timezone value from php.ini, which may differ between your local machine, staging, and production. That mismatch is a classic source of "the time is wrong by a few hours" bugs.
Calling date_default_timezone_set() early in your script makes the timezone explicit and consistent everywhere your code runs, no matter how the server is configured.
Basic usage
<?php
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s');This sets the script's timezone to U.S. Eastern Time. Every subsequent date/time call now renders in that zone. Setting the zone does not change the underlying timestamp — only how it is displayed.
Switching timezones at runtime
You can change the default timezone more than once. The same Unix timestamp shows a different wall-clock time in each zone:
Here date_default_timezone_get() confirms the zone currently in effect — useful when debugging.
Handling an invalid identifier
Because the function returns false (and warns) on a bad identifier, validate the return value when the zone comes from user input or configuration:
<?php
$zone = 'Mars/Olympus_Mons'; // not a real timezone
if (@date_default_timezone_set($zone)) {
echo "Timezone set to $zone\n";
} else {
date_default_timezone_set('UTC');
echo "Invalid timezone, falling back to UTC\n";
}
// Outputs: Invalid timezone, falling back to UTCCommonly used identifiers
PHP supports hundreds of zones. A few frequently used ones:
| Region | Identifier |
|---|---|
| New York | America/New_York |
| Chicago | America/Chicago |
| Los Angeles | America/Los_Angeles |
| London | Europe/London |
| Paris | Europe/Paris |
| Tokyo | Asia/Tokyo |
| Sydney | Australia/Sydney |
| Coordinated Universal Time | UTC |
For the complete, machine-generated list use timezone_identifiers_list(), and see the PHP timezones reference for an overview.
Good practices
- Set it once, early. Call it near the top of your bootstrap/entry file so the whole request shares one zone.
- Prefer storing UTC. Save timestamps in UTC in the database and convert to the user's zone only for display; this avoids ambiguity around daylight saving time.
- Use identifiers, not abbreviations.
Europe/Londonaccounts for DST automatically;GMT/BSTdo not.
Conclusion
date_default_timezone_set() gives you explicit control over how PHP renders dates and times, keeping behaviour consistent across environments. Set it deliberately, validate untrusted identifiers, and confirm the active zone with date_default_timezone_get() when something looks off.