php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75012 strtotime('-x month') has an unexpected behavior
Submitted: 2017-07-31 20:57 UTC Modified: 2017-07-31 21:05 UTC
From: paladinotoldo at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.0.21 OS: any
Private report: No CVE-ID: None
 [2017-07-31 20:57 UTC] paladinotoldo at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.strtotime
---

 strtotime('-1 month') has an unexpected behavior in 31th day of month

Test script:
---------------
$t =strtotime('31-07-2017');
echo "\n". date('d/m/Y',$t );
$timestamp = strtotime('-1 month',$t );
echo "\n".date("d/m/Y", $timestamp);
$timestamp = strtotime('-2 month',$t );
echo "\n". date("d/m/Y", $timestamp);
$timestamp = strtotime('-3 month',$t );
echo "\n". date("d/m/Y", $timestamp);
$timestamp = strtotime('-4 month',$t );
echo "\n". date("d/m/Y", $timestamp);
$timestamp = strtotime('-5 month',$t );
echo "\n". date("d/m/Y", $timestamp);




Expected result:
----------------
31/07/2017
30/06/2017
31/05/2017
30/04/2017
31/03/2017
28/02/2017

or 

31/07/2017
01/07/2017
01/06/2017
02/05/2017
02/04/2017
03/03/2017

Actual result:
--------------
31/07/2017
01/07/2017
31/05/2017
01/05/2017
31/03/2017
03/03/2017

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-07-31 21:05 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2017-07-31 21:05 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

"-X month" just removes X from the month number. "-1 month" with 2017-07-31 makes it 2017-06-31, which does not exist, and hence rolls over to 2017-07-01. This is expected and proper behaviour.

See also: https://twitter.com/rasmus/status/892070940648943616
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Sep 25 17:05:53 2022 UTC