[PHP-NOTES] note 130190 added to datetime.examples-arithmetic

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
Be careful when subtracting a duration crossing a DST change, it can output a resulting date AFTER your base date.

// tested in 8.3.18, 7.4.33

// DST timezone shift is applied properly when setting time explicitely

echo (new DateTime('2025-03-30'))->setTimeZone(new DateTimeZone('Europe/Paris'))->setTime(1,59,0)->format('c').PHP_EOL;
echo (new DateTime('2025-03-30'))->setTimeZone(new DateTimeZone('Europe/Paris'))->setTime(2,0,0)->format('c').PHP_EOL;
echo (new DateTime('2025-03-30'))->setTimeZone(new DateTimeZone('Europe/Paris'))->setTime(2,1,0)->format('c').PHP_EOL;

// 2025-03-30T01:59:00+01:00 < correct
// 2025-03-30T03:00:00+02:00 < correct
// 2025-03-30T03:01:00+02:00 < correct

echo PHP_EOL;

// DST timezone shift is applied properly when addind a duration

$startDateTime = (new DateTime('2025-03-30'))->setTimeZone(new DateTimeZone('Europe/Paris'))->setTime(1,58,0);
echo $startDateTime->format('c').PHP_EOL;
$endDatetime = clone($startDateTime)->add(DateInterval::createFromDateString('4 minutes'));
echo $endDatetime->format('c').PHP_EOL;

// 2025-03-30T01:58:00+01:00 < correct
// 2025-03-30T03:02:00+02:00 < correct

echo PHP_EOL;

// DST timezone shift is applied improperly when subtracting a duration

$startDateTime = (new DateTime('2025-03-30'))->setTimeZone(new DateTimeZone('Europe/Paris'))->setTime(3,2,0);
echo $startDateTime->format('c').PHP_EOL;
$endDatetime = clone($startDateTime)->sub(DateInterval::createFromDateString('4 minutes'));
echo $endDatetime->format('c').PHP_EOL;

// 2025-03-30T03:02:00+02:00 < correct
// 2025-03-30T03:58:00+02:00 < incorrect !!!

echo PHP_EOL;

// DST timezone shift is still applied improperly when adding a negative duration

$startDateTime = (new DateTime('2025-03-30'))->setTimeZone(new DateTimeZone('Europe/Paris'))->setTime(3,2,0);
echo $startDateTime->format('c').PHP_EOL;
$endDatetime = clone($startDateTime)->add(DateInterval::createFromDateString('-4 minutes'));
echo $endDatetime->format('c').PHP_EOL;

// 2025-03-30T03:02:00+02:00 < correct
// 2025-03-30T03:58:00+02:00 < incorrect !!!
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 194.177.53.153)
----
Manual Page -- https://php.net/manual/en/datetime.examples-arithmetic.php
Edit        -- https://main.php.net/note/edit/130190
Del: integrated  -- https://main.php.net/note/delete/130190/integrated
Del: useless     -- https://main.php.net/note/delete/130190/useless
Del: bad code    -- https://main.php.net/note/delete/130190/bad+code
Del: spam        -- https://main.php.net/note/delete/130190/spam
Del: non-english -- https://main.php.net/note/delete/130190/non-english
Del: in docs     -- https://main.php.net/note/delete/130190/in+docs
Del: other reasons-- https://main.php.net/note/delete/130190
Reject      -- https://main.php.net/note/reject/130190
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.