last day (15 days later) » 

10:58 PM
0
Q: PHP DateTime does not correctly calculate time component when adding across DST boundaries

XenologyConsider the following code $participantTriggerDate = new \DateTime('2019-06-14 09:40:00.000000', new DateTimeZone('US/Central')); $dateInterval = new \DateInterval('P150D'); $newDate = \DateTimeImmutable::createFromMutable($participantTriggerDate); $newDate = $newDate->add($dateInterval); ech...

 
DateInterval() adjusts for DST, so if you say that it should be 0H, it will be the same time of day, regardless of DST.
This is usually what one wants. When you say you want to do something N days later, it means at the same time of day.
 
@Barmar I figured by not specifying a time component, that PHP would know that adding 150 days to the date would result in crossing a DST boundary, and would appropriately adjust the time. This doesn't seem to be the case. Is there something I need to add to the DateInterval string to get PHP to calculate the time component properly? In this current form, its obvious that PHP's DateInterval is not calculating DST changes correctly when added to a date.
 
Why do you say it's not adjusting? The time is 09:40 in both dates.
 
Because 150 days after 2019-06-14 in the US/Central timezone passes a Daylight Savings Time boundary meaning that adding 150 days results in the timezone falling back an hour. So technically speaking, +150 days to this date is actually +149 days and 23 hours.
Because DST is not explicit across locations and timezones, I'm trying to figure out why PHP doesn't handle this under the hood for me as I assumed it did.
 
The point is that it adjusts automatically, so you don't notice the extra or missing hour. 150 days is not the same as 150*24 hours.
 
10:58 PM
My point is that it does not adjust the time accordingly. If you run the code above you will see it does not adjust the time component of the newDate to reflect that it is in a different DST for that timezone. If PHP were doing this conversion under the hood for me newDate would have a time of 8:40:00
 
If it had a time of 8:40 it wouldn't be the same time, it would be an hour different. That's what you would get if you added 3600 hours instead of 150 days.
Notice that the timezone is US/Central. That means that times are shown in whatever is the appropriate state of daylight or standard time applies on the date, they're not shown in the current timezone.
 
I don't think you're understanding what I am saying here. In some places, DST happens, and in others it doesn't. When setting timezones on a PHP date object, PHP automatically determines the changes across DST. If you convert from a timezone thats in DST to a timezone thats out of DST. PHP will do this math for you and return the proper time. In this case, I am adding a date interval that causes the DATE to change from inside DST, to outside DST. Adding 150 days to the date means that the time has actually changed in that timezone.
 
I understand that. And what I'm trying to tell you is that when it sees that it crosses a DST boundary, it automatically adds or subtracts an hour to keep the time of day the same, because adding multiples of days shouldn't change the time.
 
I was hoping that DST calculations happened automatically when adding a date interval that caused a date object to cross a DST boundary. As maintaining this myself would be problematic and prone to breakage. Adding 150 days to this date isn't adding exactly 150 days because of DST
 
You seem to have it backwards. It IS adding exactly 150 days because it adjusts automatically.
It's not adding 150*24 hours, it's adding 150*24+1 hours to adjust for DST changing.
 
11:00 PM
2019-06-14 09:40:00 + 150 days is not 2019-11-11 09:40:00 That is 150 days and 1 hour.
 
Let's say the start time is 09:40 on the day before DST changes. You ask to add 1 day, you get 09:40 on the next day.
 
I don't think PHP is doing anything under the hood to "calculate" the extra hour and hide it
 
Your result showed that it does, the time is 09:40 in both cases.
 
My point is that is not a correct representation of adding 24 hours or 1 day
I believe what PHP does is that it literally just says whats 150 days forward and then slaps the time component onto that,
 
That's right. That's what it's supposed to do.
That's what i mean by automatically adjusting for DST.
 
11:03 PM
This is not the functionality I need, I need DST boundaries to be calculated when crossing them
 
Then add hours instead of days.
 
I cannot do that in this case
 
You're treating every day as exactly 24 hours. But the days when DST changes are either 25 or 23 hours long.
 
So in the case of adding 150 days worth of hours
 
If I have an appointment at 5:30 every day, it doesn't change to 04:30 when DST changes.
 
11:05 PM
Would PHP handle this case correctly?
 
I think it will do what you want, but test it.
Add P3600H
 
Please put that as the answer for the question so that I can accept it.
This appears to do what I need it to do,
 

last day (15 days later) »