Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeZone error: Unknown or bad timezone

Tags:

php

I am trying to run this script:

<?php
$d = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$time = $d->format('H:i');

echo $time;
?>

but I got this error:

Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct() [<a href='function.DateTimeZone---construct'>function.DateTimeZone---construct</a>]: Unknown or bad timezone (Asia/Kolkata)

Though it works well for Asia/Dacca for example. What could be the problem and how to fix it?

like image 405
Falya Avatar asked Jan 31 '11 06:01

Falya


3 Answers

And welcome to StackOverflow! If you haven't already make some time to read the FAQ.

I tried your example and it worked for me with PHP 5.3.1 and "Olson" Timezone Database Version 2009.18 .

You should do a phpinfo() and see what time zone DB version you have and if it's older update it. You can see in this list that the most recent version, 2011.1, has Asia/Kolkata.

like image 102
Alin Purcaru Avatar answered Oct 24 '22 07:10

Alin Purcaru


I've got same problem when try to run webserver in CHROOT jail. In this case chrooted dir can not contain Linux timezone files. If it's your case and you have access to webserver files simple copy all files to chroot jail:

For CentOS it's

cp -R /usr/share/zoneinfo/ /MY_CHROOT_DIR/usr/share/
like image 36
Alex Avatar answered Oct 24 '22 09:10

Alex


Try this for fun.

$timezones = array('Europe/London', 'Mars/Phobos', 'Asia/Kolkata');

foreach ($timezones as $tz) {
    try {
        $mars = new DateTimeZone($tz);
    } catch(Exception $e) {
        echo $e->getMessage() . '<br />';
    }
}
like image 2
Elzo Valugi Avatar answered Oct 24 '22 08:10

Elzo Valugi