Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid DateTime::modify with negative offset in PHP 8.2 #10228

Closed
pavol-tuka opened this issue Jan 5, 2023 · 5 comments
Closed

Invalid DateTime::modify with negative offset in PHP 8.2 #10228

pavol-tuka opened this issue Jan 5, 2023 · 5 comments

Comments

@pavol-tuka
Copy link

pavol-tuka commented Jan 5, 2023

Description

DateTime::modify or DateTimeImmutable::modify in PHP 8.2 returns invalid result with negative offset.

$now = new DateTimeImmutable(); // 2023-01-05

$offset = -2;
var_dump($now->modify('+' . $offset . ' days')->format('Y-m-d'));

$offset = 2;
var_dump($now->modify('+' . $offset . ' days')->format('Y-m-d'));

PHP 8.1.14 output:

string(10) "2023-01-03"
string(10) "2023-01-07"

PHP 8.2.1 output:

string(10) "2023-01-07" <-- 2023-01-03 expected
string(10) "2023-01-07"

PHP Version

PHP 8.2.1

Operating System

Linux Mint 21.1

@cmb69
Copy link
Member

cmb69 commented Jan 5, 2023

I can confirm the behavioral change: https://3v4l.org/vMf7Q

We should probably keep BC here, although +-2 is strange, and there are ways to properly handle that in userland.

@derickr
Copy link
Member

derickr commented Jan 5, 2023

This was a deliberate change, to work around INT_MAX / INT_MIN issues: https://github.com/derickr/timelib/pull/132/files

@hormus
Copy link

hormus commented Jan 5, 2023

#9950 duplicate #10228
DateTimeImmutable::__construct php >= 5.5.0

<?php

$now = new DateTimeImmutable(); //Maybe local timezone 2023-01-05

$days = -2;
if($days) {
$string = str_replace(array('++', '+-'), array('+', '-'), '+' . $days);
var_dump($now->modify(str_replace('-', '+', $string) . ' day')->format('Y-m-d')); // Force plus sign
}

$days = 2;
if($days) {
$string = str_replace(array('++', '+-'), array('+', '-'), '+' . $days);
var_dump($now->modify($string . ' day')->format('Y-m-d'));
}
?>

@cmb69
Copy link
Member

cmb69 commented Jan 5, 2023

Oh, indeed, duplicate of #9950.

@cmb69 cmb69 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2023
@cmb69
Copy link
Member

cmb69 commented Jan 5, 2023

FWIW, the behavior matches the docs now: number | [+-]?[0-9]+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants