Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dan3460's avatar

Carbon difference with different timezones

Working on a application that has dates and times on different timezones. I need to compare the "projected_arrival" to the "time_window_close". The time_window_close is on EST and the projected arrival on GMT. Not matter what i'm doing keeps giving me the same result. Applying ->setTimezome() does show the projected arrival on the correct time so it has to be a way to compare the "correct" time with the window.

Psy Shell v0.11.2 (PHP 8.0.10 — cli) by Justin Hileman
>>> $s=Stop::find(673195)
[!] Aliasing 'Stop' to 'App\Models\Stop' for this Tinker session.
=> App\Models\Stop {#4076
     id: 673195,
     round_id: 91546,
     planned_sequence: 12,
     planned_arrival: "2022-03-24 16:28:09",
     projected_arrival: "2022-03-24 16:28:09",
     location_id: 26050,
     size1: 37.77,
     time_window_open: "2022-03-24 10:00:00",
     time_window_close: "2022-03-24 14:00:00",
     projected_time_window_missed: -148,
     stop_reason_id: 1,
     show: "1",
     created_at: "2022-03-24 17:25:07",
     updated_at: "2022-03-24 17:25:07",
   }
>>> $s->projected_arrival->diffInMinutes($s->time_window_close,false)
=> -148
>>> $s->projected_arrival->setTimezone('America/New_York')->diffInMinutes($s->time_window_close,false)
=> -148
>>> $s->projected_arrival->setTimezone('America/New_York')
=> Illuminate\Support\Carbon @1648139289 {#4692
     date: 2022-03-24 12:28:09.0 America/New_York (-04:00),
   }
0 likes
3 replies
sr57's avatar

compare the "correct" time

Of "correct" depends of you want

  • setTimezone changes the hour

  • shiftTimezone does not change it

dan3460's avatar

The projected arrival on EST is 12:28, the window closes at 14:00, the difference should read 92 (positive number). No matter how i try to use carbon i keep get -148 (which is arrival 16:28 in GMT and window 14:00, 2 hours and 28 minutes late). I have a long way to calculate, but i was looking for a more elegant way to do it.

dan3460's avatar

I think i understand my problem now. I thought that when i created the window close from the data (which only shows the time) it was been created in the est time zone, but it is always created in utc time, how i can force the system to create the variable as est time?

>>> $s->time_window_close=Carbon\Carbon::createFromTimeString('13:00','America/New_york')
=> Carbon\Carbon @1648227600 {#4562
     date: 2022-03-25 13:00:00.0 America/New_york (-04:00),
   }
>>> $s->time_window_close
=> Illuminate\Support\Carbon @1648213200 {#4707
     date: 2022-03-25 13:00:00.0 UTC (+00:00),
   }
>>> $s->time_window_close=Carbon\Carbon::createFromTimeString('13:00')->setTimeZone('America/New_York')
=> Carbon\Carbon @1648213200 {#3758
     date: 2022-03-25 09:00:00.0 America/New_York (-04:00),
   }
>>> $s->time_window_close
=> Illuminate\Support\Carbon @1648198800 {#4709
     date: 2022-03-25 09:00:00.0 UTC (+00:00),
   }
>>>

Please or to participate in this conversation.