I have a website that displays events from the website owner's Facebook page. A few weeks ago, someone noticed that the event times are showing up wrong on the website, but they've been correct for a couple years. So basically I'm trying to figure out what the problem is.
Here's an example
Event X has a start time timestamp of 2017-12-18T17:00:00-0500. That date is correct, and that time - 1700, or 5:00 - is the correct time.
So I have this code to convert the timestamp to something I can display
$start_time = date('g:i a', strtotime($event['start_time']));
This returns a time of 10:00 PM
I have the same problem with the end time not converting correctly.
I'm using this code to convert the date (using the same timestamp above):
$start_date = date('l, F j, Y', strtotime($event['start_time']));
This returns the correct date, which confuses me even more because if the date converts correctly, how does the time not convert correctly?
Can someone please help me get the time to convert so I can get these events back on the website?
It is different timezone problem.
You have a datetime with timezone -05:00
but, date('P')
output, your server timezone is +00:00
. You can use DateTime
class to convert the datetime to the timezone your desired.
// convert time to datetime instance
$timestamp = strtotime($event['start_time']);
$datetime = new DateTime;
$datetime->setTimestamp($timestamp);
// set timezone to US/Eastern, Eastern Standard Time (EST), UTC -5
$datetime->setTimezone(new DateTimeZone('US/Eastern'));
// output datetime format
$datetime->format('g:i a');
$datetime->format('l, F j, Y');