php-src icon indicating copy to clipboard operation
php-src copied to clipboard

DateTime->modify produces different results in php8.2 than in php8.1

Open samuelvi opened this issue 3 years ago • 2 comments

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

https://3v4l.org/6u9k0

samuelvi avatar Feb 21 '23 10:02 samuelvi

@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

pospjan avatar Feb 21 '23 11:02 pospjan

Thank you for your kindly response and sorry about that.

samuelvi avatar Feb 21 '23 12:02 samuelvi