Bug #76038 [Com]: strtotime returning wrong date on -1 month

From: Date: Thu, 01 Mar 2018 16:20:47 +0000
Subject: Bug #76038 [Com]: strtotime returning wrong date on -1 month
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214170@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76038&edit=1

 ID:                 76038
 Comment by:         diego at agudo dot eti dot br
 Reported by:        diego at agudo dot eti dot br
 Summary:            strtotime returning wrong date on -1 month
 Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 Operating System:   Windows / Linux
 PHP Version:        7.2.2
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2018-03-01 15:32:00] 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 15:03:13] 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 14:28:13] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=76038&edit=1


Thread (5 messages)

« previous php.bugs (#214170) next »