The code you posted parses the string '15:00:00 +03:00
.
In plain English, this string means "the local time is 15:00 (3 PM) and I am located in a timezone that is 3 hours at the East of GMT/UTC". The corresponding UTC time is 12:00
.
It happens that your PHP has the default timezone set to UTC (I guess this is the default value in php.ini
and you didn't change it). This is why date()
prints 07 Sep, 2016 12:00 PM
.
For me your code prints 07 Sep, 2016 03:00 PM
because I am also in a timezone at UTC+3
and my php.ini
uses it as default.
Don't use date()
(or any of the date/time functions). Some of them don't know anything about the timezone, others work only with local time.
Use the DateTime
classes instead. They can handle the timezones and the code is shorter and more clear.
Your concrete problem is easy to solve:
// Parse the input time in the UTC timezone
$date = new DateTime("15:00:00", new DateTimeZone("UTC"));
// Change the timezone to local
$date->setTimeZone(new DateTimeZone("+03:00"));
// Output the local time
echo($date->format("d M, Y h:i A"));
It displays:
07 Sep, 2016 06:00 PM
Read more about the DateTime
, DateTimeZone
, DateTimeInterval
and the other DateTime classes. They are the modern way of handling date & time in PHP.