php-src
php-src copied to clipboard
DateTime->modify produces different results in php8.2 than in php8.1
Description
The following code:
<?php
// You can test it here: https://3v4l.org/6u9k0
$dt = new DateTime('2015-01-01 00:00:00+00:00');
var_dump($dt->format('c'));
var_dump($dt->modify('+-2 days')->format('c'));
$dt = new DateTimeImmutable('2015-01-01 00:00:00+00:00');
var_dump($dt->format('c'));
var_dump($dt->modify('+-2 hours')->format('c'));
Resulted in this output:
string(25) "2015-01-01T00:00:00+00:00"
string(25) "2015-01-03T00:00:00+00:00"
string(25) "2015-01-01T00:00:00+00:00"
string(25) "2015-01-01T02:00:00+00:00"
But I expected this output instead:
string(25) "2015-01-01T00:00:00+00:00"
string(25) "2014-12-30T00:00:00+00:00"
string(25) "2015-01-01T00:00:00+00:00"
string(25) "2014-12-31T22:00:00+00:00"
PHP Version
8.2.1
Operating System
Gentoo Linux 2.9 - Ubuntu 22.04.2 LTS (Jammy Jellyfish)
Testing code Url
@samuelvi +-2 (multiple signs) no longer works as a modifier in php 8.2, it's a documented BC break, see https://www.php.net/manual/en/migration82.incompatible.php
number symbols in relative formats no longer accept multiple signs, e.g. +-2.
Also mentioned in the changelog here https://www.php.net/manual/en/datetime.formats.relative.php
Thank you for your kindly response and sorry about that.