I would like to change the timezone of a time like:
2015-08-24 01:30:40
so that it's in the America/Los_Angeles
timezone, but the code below isn't working as expected:
$timedate = date_timezone_set(
date_create_from_format('Y-m-d h:i:s A', $row[timezone]),
new DateTimeZone('America/Los Angeles')
);
$row[timezone]
successfully returns 2015-08-24 01:30:40
on it's own, but when I do it like this to try to change the timezone, it doesn't work.
All PHP code after that doesn't run. I have done lots of searching, but I can't figure out how to get this to work properly so can someone tell me what I am doing wrong?
Your call to date_create_from_format includes an A at the end of the format string - that looks specious to me, given that your value doesn't end with am or pm. Your use of h
looks unlikely to be correct too, as that's for a 12-hour hour-of-day, which isn't useful when you don't have an am/pm indicator. I suspect you want a format of
Y-m-d H:i:s
That appears to match your sample string better...
Additionally, as noted in comments, your time zone ID should be 'America/Los_Angeles'
rather than 'America/Los Angeles'
.
I would also separate the various calls you have in this single large line of code, to make it easier to diagnose - that way you could tell that it's the parsing that's failing, rather than anything to do with the time zone. You have three separate operations here:
Keeping those three in separate statements will make the code easier to read and easier to maintain. (In particular, you'd be able to find and correct these two problems independently...)
See more on this question at Stackoverflow