I know how to check the transitions in the year, with
$to = (clone $from)->addYear;
$arrTrans = $tz->getTransitions($from->timestamp, $to->timestamp);
The first element of the array is always the current day. Here it will return 3 elements, the current day, and the 2 days that change
So, if I want to know if the current day is a transition, and I do:
$to = (clone $from)->addDay();
$arrTrans = $tz->getTransitions($from->timestamp, $to->timestamp);
I will always get the current day, but it will not tell me if it is the transition day.
Is it really mandatory to compare with last day, and check that isdst
attribute is different or is there a nice way to do it ?
CodePudding user response:
I ended up searching for the last sunday of march and the last sunday of october.
If this is the current day, I would respectively delete the last hour, or adding an extra hour, copying the latest value I have.
Did not find better way to do it.
CodePudding user response:
Just checking whether there is a change between winter / summer time on a certain date is not difficult. If that day is summer or winter time, the following day is different.
function isDayChangeCETtoCEST(DateTimeInterface $dt){
$dayC = (clone $dt)->setTime(0,0,0);
$flag0 = $dayC->format('I');
$flag1 = $dayC->setTime(24,0,0)->format('I');
return $flag0 == 0 AND $flag1 == 1;
}
function isDayChangeCESTtoCET(DateTimeInterface $dt){
$dayC = (clone $dt)->setTime(0,0,0);
$flag0 = $dayC->format('I');
$flag1 = $dayC->setTime(24,0,0)->format('I');
return $flag0 == 1 AND $flag1 == 0;
}
How are the functions used?
$date = new DateTime('2021-10-31');
$isChange = isDayChangeCESTtoCET($date); //bool(true)