Skip to content
Home » Web » PHP » How to Resolve Unknown or bad timezone

How to Resolve Unknown or bad timezone

DateTimeZone Construct

One good example provided by PHP documentation can demonstrate this error:

<?php
// Error handling by catching exceptions
$timezones = array('Europe/London', 'Mars/Phobos', 'Jupiter/Europa');

foreach ($timezones as $tz) {
    try {
        $mars = new DateTimeZone($tz);
    } catch(Exception $e) {
        echo $e->getMessage() . '<br />';
    }
}
?>

The first timezone is a valid one, but not for the second and the third ones which generates the following error:

DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Mars/Phobos)
DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Jupiter/Europa)

Of course, no one lives in Mars or Jupiter.

If you got an error like this, the most possible mistake you made could be that you pass an invalid timezone as the argument of constructor. If you don't recognize all the valid timezones, don't worry, you can list all the supported timezones by:

<?php
print_r(DateTimeZone::listIdentifiers());
?>

There is another possibility, if you are sure the timezone is valid and you pass the timezone by a session value like this:

$timezone = new DateTimeZone($_SESSION['timezone']);

I guess that you had ever passed an empty or invalid value, but you don't know when or where. In such case, restarting PHP engine could not be helpful, you may insert destroySession() in you code to erase the cache completely.

For more about class DateTimeZone, you can refer to PHP manual: PHP: DateTimeZone::__construct - Manual.

Leave a Reply

Your email address will not be published. Required fields are marked *