How do I convert the timestamp 2015-06-05 14:05:01
to another timezone using php?
I've read and tried numerous ways listed on here but I cannot get the desired result. Using date_format($date,"M d h:i A")
and date_default_timezone_set('America/New_York')
I get June 05 2:05 PM
which is the original origin of the server timezone and correct.
What I need is to convert 2015-06-05 14:05:01
using for example date_default_timezone_set('America/Los_Angeles')
and date_format($date,"M d h:i A")
to get the result June 05 11:05 AM
.
Use DateTime()
with DateTimeZone()
:
// Create the DateTime() object and set the timezone to 'America/New_York'
$date = new DateTime('2015-06-05 14:05:01', new DateTimeZone('America/New_York'));
// Change the timezone to 'America/Los_Angeles'
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
// Print out the date and time in the new timezone
echo $date->format('M d h:i A');
Easy to read which makes it easy to mantain.