Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

DateTimeZone::__construct(): Unknown or bad timezone

ErrorDateTimeZone::__construct(): Unknown or bad timezone (Mars/Phobos)

Short answer

PHP throws this when the string is not a valid timezone identifier, UTC offset or abbreviation. Print the exact value the code received. The usual causes are a typo or empty value from user input or a database, an invalid date.timezone line in php.ini, or an old timezone database that lacks a newer identifier.

  • Applies to PHP 8.2, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact string that reaches the constructor, with quotes, so empty values and stray spaces show up. Then compare it with the list PHP knows.

    var_dump($tz);
    var_dump(in_array($tz, DateTimeZone::listIdentifiers(), true));
  2. 2Match what you see, then jump to its fix

    1. 1The message in parentheses shows a wrong name, or nothing at allThe value is misspelled, empty or comes from user inputFix
    2. 2The error or a warning shows on every request, even in code that never passes a timezonedate.timezone in php.ini holds an invalid valueFix
    3. 3A valid, recent identifier is rejected, while the same code works on another serverThe timezone database is too old for the identifierFix

What does the error look like?

  • DateTimeZone::__construct(): Unknown or bad timezone (Mars/Phobos)
  • Uncaught DateInvalidTimeZoneException (PHP 8.3 and later)

Cause 1 of 3

The value is misspelled, empty or comes from user input

1Check

The constructor accepts IANA identifiers such as Europe/London, UTC offsets such as +0200 or -06:00, and abbreviations such as CEST. Anything else fails. Print the value and look for case or spelling errors, or an empty string from a missing setting or form field.

var_dump($tz);
print_r(DateTimeZone::listIdentifiers(DateTimeZone::EUROPE));

2Fix

Validate the value against the identifier list before you build the object, and fall back to a default when it is not valid.

$tz = in_array($input, DateTimeZone::listIdentifiers(), true) ? $input : 'UTC';
$zone = new DateTimeZone($tz);

3Verify

Pass a bad value on purpose; the code should use the fallback and not throw.

php -r '$z = new DateTimeZone("Europe/London"); echo $z->getName();'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

date.timezone in php.ini holds an invalid value

1Check

From PHP 8.2 a warning is emitted when date.timezone is set to an invalid value or an empty string. Read the configured value and the zone PHP resolves.

php -r 'var_dump(ini_get("date.timezone"), date_default_timezone_get());'
php --ini

2Fix

Set date.timezone to a valid identifier in the php.ini that php --ini lists, or remove the line to use the default of UTC.

; php.ini
date.timezone = "Europe/London"

3Verify

Restart PHP-FPM or Apache and re-run the check.

php -r 'echo date_default_timezone_get();'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The timezone database is too old for the identifier

1Check

timezone_version_get() returns the version of the timezonedb in use, in the form YYYY.increment. A value that does not show the current year means old data. The result 0.system means PHP is using the operating system's tzdata.

php -r 'echo timezone_version_get();'

2Fix

Upgrade PHP, or install the timezonedb PECL package. If the version is 0.system, PHP uses the operating system's tzdata, so update that through your package manager.

sudo pecl install timezonedb

3Verify

Check the version again, then construct the zone that failed.

php -r 'echo timezone_version_get(), PHP_EOL; echo (new DateTimeZone("Europe/London"))->getName();'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Store timezones as IANA identifiers and validate them when a user saves a profile.
  • Catch Exception around the constructor, or use timezone_open() which returns false instead of throwing.
  • Set date.timezone explicitly in php.ini and keep a copy in your server setup notes.
  • Keep tzdata and PHP updated on every server that runs the same code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.