Merbin Jo Merbin Jo - 1 month ago 13
PHP Question

How to convert Localtime to user seleted timezone in php

I want to convert the India time to user selected timezone in php. I used the following php code, but it showing some error " Uncaught exception 'Exception' with message 'DateTimeZone::__construct(): Unknown or bad timezone (+13:40)'".



My php code

$date = new DateTime('2016-10-22 01:39:53 +5:30');
$date->setTimezone(new DateTimeZone('+13:40'));
echo $date->format('Y-m-d H:i:s');


But this code is working perfectly in phpfiddle.

Working link here http://phpfiddle.org/main/code/5vi2-r7t9

Answer

That's likely a PHP version problem. From my tests it seems that PHP versions before 5.5.10 do not support the +nn:nn timezone format:

See https://3v4l.org/C8V2l

Try replacing it with a named timezone from the List of Supported Timezones or upgrade your PHP version.

Comments