date_default_timezone_get

Returns the timezone identifier currently used by all date and time functions in the script.

Signature
date_default_timezone_get(): string
  • Added in PHP 5.1.0

Return value

stringThe active timezone identifier, such as "UTC", "Europe/London" or "America/Los_Angeles".

Errors

Does not throw.

Changelog

  • PHP 5.4.0PHP no longer guesses the timezone from the TZ environment variable or the host operating system. If date.timezone is unset and nothing has called date_default_timezone_set(), the answer is UTC.

date_default_timezone_get reports the timezone that date, mktime, strtotime and every DateTime object created without an explicit zone will use. It resolves in a fixed order: the last successful date_default_timezone_set call, then the date.timezone ini setting, then UTC.

When to reach for it

Two situations. First, diagnostics — printing this value at the top of a debug page instantly explains why timestamps render an hour off, and it is far more reliable than reading ini_get('date.timezone'), which returns an empty string when the setting was never configured. Second, save-and-restore: if a piece of code has to switch the default timezone temporarily (rendering one report in the customer's zone, say), capture the current value first and set it back afterwards.

Note that the fallback to UTC is silent. A configuration mistake does not produce an error here, it produces a plausible-looking wrong timezone. That makes an explicit date_default_timezone_set during application bootstrap worthwhile even when UTC is what you want.

The return value is always a full identifier string, never an offset like +02:00 and never an abbreviation like CEST.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Printing the active timezone on a health-check or debug page to explain off-by-an-hour timestamps
  • Saving the current timezone before switching it to render a report, then restoring it
  • Passing the script's timezone into a DateTimeZone when constructing a DateTimeImmutable explicitly

Frequently asked questions

What does date_default_timezone_get return if no timezone is configured?

UTC. Since PHP 5.4 the engine no longer guesses from the operating system, so an unconfigured install silently reports UTC.

Is date_default_timezone_get the same as ini_get('date.timezone')?

No. ini_get returns an empty string when the setting was never written, while date_default_timezone_get always returns the identifier actually in use, including the UTC fallback and anything set at runtime.

Does it return an offset like +02:00?

No, it returns a timezone identifier such as Europe/Berlin. To get the offset, build a DateTimeZone from it and format a date with the P or Z character.

More examples

Switch the timezone temporarily, then put it back

The default timezone is global state, so anything that changes it must change it back. Capturing the previous value first makes the swap safe for the rest of the request.

Example · php
<?php
date_default_timezone_set('UTC');
$ts = 1751630400;

$previous = date_default_timezone_get();
date_default_timezone_set('Europe/Berlin');
echo 'customer view: ', date('Y-m-d H:i T', $ts), "\n";
date_default_timezone_set($previous);

echo 'restored to:   ', date_default_timezone_get(), "\n";
echo 'system view:   ', date('Y-m-d H:i T', $ts), "\n";

Detecting the silent UTC fallback

The active zone can be UTC either because someone chose it or because nothing was configured at all. Comparing against the ini value tells the two cases apart.

Example · php
<?php
$active = date_default_timezone_get();
$configured = ini_get('date.timezone');

if ($configured === '' || $configured === false) {
    echo "date.timezone is not configured; PHP defaulted to {$active}\n";
} else {
    echo "date.timezone is set to {$configured}, active zone is {$active}\n";
}

Discussion

  • Be the first to comment on this lesson.