DateTimeZone::__construct(): Unknown or bad timezone
DateTimeZone::__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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
The message in parentheses shows a wrong name, or nothing at allThe value is misspelled, empty or comes from user inputFix - 2
The 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
A valid, recent identifier is rejected, while the same code works on another serverThe timezone database is too old for the identifierFix
- 1
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
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 --ini2Fix
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
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 timezonedb3Verify
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix