I’m trying to convert an epoch into a human friendly date-time. I’m off by 1 hour. Why?
$aptTime = 1694194200;
if(!empty($aptTime)) {
$aptDisplay = new dateTime("@$aptTime");
$aptDisplay->setTimezone(new DateTimeZone('EST'));
$aptStr = date_format($aptDisplay, "l F jS a\t g:i A");
$html = '<p class="feature-box-desc text-dark" id="apt"><strong>' . $aptStr . '</strong></p>';
}
Expected Output: Friday September 8th at 1:30 PM
Generated Output: Friday September 8th at 12:30 PM
2
Answers
If you don’t want acknowledge Daylight Saving Time
This would get you the desired output
Or if you acknowledge DST, then you solution is accurate.
1694194200 is Friday, 8 September 2023 17:30:00 UTC according to Epoch Converter. PHP agrees:
EST
stands for Eastern Standard Time, which is UTC−05:00. So the expected local time is exactly the one you get, 12:30.I assume you expect 13:30 because your clock is set to Eastern Standard Time and that’s the time it shows. The problem is that your clock is actually set to Eastern Daylight Time (EDT), which is UTC−04:00.
The root cause is that daylight saving time works by actually switching the affected territory to a different time zone, twice a year. So using these fixed time zones is not useful at all. Instead, I recommend you set geographical identifiers, which do not change across the year and are backed by a database that keeps track of UTC offset changes:
Replace New York with the appropriate major city.