Skip to content
Tutorials Center
My learning

Topics

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
<?php
date_default_timezone_set("Europe/Paris");
echo date_default_timezone_get();   // Europe/Paris

Try it

PHP One timestamp, three zones

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.

PHP Invalid names and daylight saving

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

PHP
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.timezone in php.ini. The function overrides the ini setting for the current script.
  • Use UTC for 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. Use DateTimeImmutable::setTimezone() or the DateTimeZone argument so one setting cannot leak into other work.
  • Validate names from user input against timezone_identifiers_list() or DateTimeZone::listIdentifiers(). Abbreviations such as EST are accepted by some APIs but are not real regions and ignore daylight saving.
  • Deprecated names. Old aliases such as US/Eastern exist for compatibility; prefer the Area/City form.
  • The zone database is bundled with PHP, so very new rule changes need a PHP update (or the timezonedb PECL package).
  • Global state. Libraries should never call it; it changes behavior for the whole process.