php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76038 strtotime returning wrong date on -1 month
Submitted: 2018-03-01 14:28 UTC Modified: 2018-03-01 16:49 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: diego at agudo dot eti dot br Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.2.2 OS: Windows / Linux
Private report: No CVE-ID: None
 [2018-03-01 14:28 UTC] diego at agudo dot eti dot br
Description:
------------
strtotime returning wrong date on -1 month

Test script:
---------------
$data = '2018-03-31';
print_r($data);
echo "\n\n";
$data = strtotime($data);
print_r($data);
echo "\n\n";
$data = date("Y-m-d", $data);
print_r($data);
echo "\n\n";
$data = strtotime($data);
print_r($data);
echo "\n\n";
$data = date("Y-m-d", strtotime('-1 month', $data));
print_r($data);
echo "\n\n";






Expected result:
----------------
2018-02-28

Actual result:
--------------
2018-03-03

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-03-01 15:03 UTC] Wes dot example at example dot org
I don't see how the suggested behavior is more correct than the current one.

When doing "month math" you are supposed to ignore the days, since months have different lengths.
 [2018-03-01 15:32 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-03-01 15:32 UTC] requinix@php.net
-1 month means subtract one from the month number. 2018-03-31 -1 month = 2018-02-31 which overflows to 2018-03-03.
 [2018-03-01 16:20 UTC] diego at agudo dot eti dot br
Sorry but I disagree, should be 2018-02-28.

Oracle/PostgreSQL/MySQL works like a charm.

If I do like you said, I lost 2~3 days.

SAMPLE MySQL:
SELECT DATE_ADD(DATE_ADD(NOW(),INTERVAL + 30 DAY), INTERVAL -1 MONTH)

ORACLE:
SELECT ADD_MONTHS(TO_DATE('2018-03-31','YYYY-MM-DD'),-1) FROM DUAL
 [2018-03-01 16:49 UTC] requinix@php.net
Good for them. But PHP and much of the Linux/GNU world do not work like that.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 09:01:29 2024 UTC