php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77220 Problem for month March (DateTime::sub)
Submitted: 2018-11-29 15:11 UTC Modified: 2018-11-29 16:35 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: info at 4md dot eu Assigned: cmb (profile)
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: info at 4md dot eu
New email:
PHP Version: OS:

 

 [2018-11-29 15:11 UTC] info at 4md dot eu
Description:
------------
If substract 1 Month for 31/30/29 march => Not correctly subtract (possible problem to February Month).

Test script:
---------------
<?php

$intervalMonth = new DateInterval('P1M');

$dates = ['2019-01-30', '2019-02-28', '2019-03-29', '2019-03-30', '2019-03-31', '2019-04-30'];
foreach($dates as $date){
    
    $d = new DateTime($date);
    
    // Substract 1 Month
    $d->sub($intervalMonth);
    $subMonth = $d->format('Y-m-d');
    
    // Adding 1 Month
    $d->add($intervalMonth);
    $addMonth = $d->format('Y-m-d');
    
    // Echo information
    if(substr($date, 0, 7) === '2019-03'){
        echo '*';
    }
    echo 'Now: '.$date.'; Sub 1Month: '.$subMonth."; Add Month: ".$addMonth."\n";
}

echo "\n";
echo '* BUG';

Actual result:
--------------
Now: 2019-01-30; Sub 1Month: 2018-12-30; Add Month: 2019-01-30
Now: 2019-02-28; Sub 1Month: 2019-01-28; Add Month: 2019-02-28
*Now: 2019-03-29; Sub 1Month: 2019-03-01; Add Month: 2019-04-01
*Now: 2019-03-30; Sub 1Month: 2019-03-02; Add Month: 2019-04-02
*Now: 2019-03-31; Sub 1Month: 2019-03-03; Add Month: 2019-04-03
Now: 2019-04-30; Sub 1Month: 2019-03-30; Add Month: 2019-04-30

* BUG

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-29 16:35 UTC] cmb@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

See <http://php.net/manual/en/datetime.sub.php#refsect1-datetime.sub-examples>, example 3.
 [2018-11-29 16:35 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2018-11-29 16:35 UTC] cmb@php.net
.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 04:01:27 2024 UTC