skip to Main Content

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


  1. If you don’t want acknowledge Daylight Saving Time

    This would get you the desired output

    $aptDisplay->setTimezone(new DateTimeZone('Etc/GMT+4'));
    

    Or if you acknowledge DST, then you solution is accurate.

    Login or Signup to reply.
  2. 1694194200 is Friday, 8 September 2023 17:30:00 UTC according to Epoch Converter. PHP agrees:

    date_default_timezone_set('UTC');
    echo date('r', 1694194200);
    

    Fri, 08 Sep 2023 17:30:00 +0000

    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.

    On the second Sunday in March, at 2:00 a.m. EST, clocks are advanced to 3:00 a.m. EDT leaving a one-hour gap. On the first Sunday in November, at 2:00 a.m. EDT, clocks are moved back to 1:00 a.m. EST, which results in one hour being duplicated. Southern parts of the zone in Panama and the Caribbean, however, do not observe daylight saving time.

    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:

    $aptTime = 1694194200;
    $aptDisplay = new dateTime("@$aptTime");
    $aptDisplay->setTimezone(new DateTimeZone('America/New_York'));
    echo $aptDisplay->format('r');
    

    Fri, 08 Sep 2023 13:30:00 -0400

    Replace New York with the appropriate major city.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search