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?
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.
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/
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 />';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With