PHP Reference ยท Date and Time
PHP date_default_timezone_set() function
Reference for PHP date_default_timezone_set() and date_default_timezone_get(): choose the default time zone, valid names, the php.ini setting and best practice.
date_default_timezone_set() chooses the time zone that PHP's date functions use when you do not name one yourself. It affects date(), mktime(), strtotime() for text without a zone, and new DateTime() without a DateTimeZone argument. date_default_timezone_get() reads the current setting.
Names come from the IANA time zone database and look like Europe/Berlin or America/New_York. They follow the region's rules, including daylight saving, unlike a fixed offset such as +01:00. The default, when nothing is configured, is UTC.
<?php date_default_timezone_set("Europe/Paris"); echo date_default_timezone_get(); // Europe/Paris
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
date_default_timezone_set(string $timezoneId): bool date_default_timezone_get(): string
| Name | Description |
|---|---|
$timezoneId |
An identifier from the IANA database, such as UTC or Europe/Rome. |
Return value: date_default_timezone_set() returns true on success. For an unknown identifier it returns false and emits an E_NOTICE; the previous setting stays. date_default_timezone_get() returns the current identifier.
Usage notes
- Set it once, early, in your bootstrap code, or set
date.timezoneinphp.ini. The function overrides the ini setting for the current script. - Use
UTCfor storage. Store timestamps or UTC values in the database and convert to the user's zone only when displaying. - Per-user zones. Do not call
date_default_timezone_set()per user in a long-running process. UseDateTimeImmutable::setTimezone()or theDateTimeZoneargument so one setting cannot leak into other work. - Validate names from user input against
timezone_identifiers_list()orDateTimeZone::listIdentifiers(). Abbreviations such asESTare accepted by some APIs but are not real regions and ignore daylight saving. - Deprecated names. Old aliases such as
US/Easternexist for compatibility; prefer theArea/Cityform. - The zone database is bundled with PHP, so very new rule changes need a PHP update (or the
timezonedbPECL package). - Global state. Libraries should never call it; it changes behavior for the whole process.
Related
date(),DateTimeZone,DateTimeImmutable,time()- Lessons: Dates and Times