php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67706 strtotime goes wrong for "last day of February next year"
Submitted: 2014-07-29 09:42 UTC Modified: 2017-03-30 07:34 UTC
Votes:10
Avg. Score:3.5 ± 1.4
Reproduced:8 of 10 (80.0%)
Same Version:3 (37.5%)
Same OS:4 (50.0%)
From: greenrover33 at gmail dot com Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 5.6Git-2014-07-29 (Git) OS: Linux/Independent
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: greenrover33 at gmail dot com
New email:
PHP Version: OS:

 

 [2014-07-29 09:42 UTC] greenrover33 at gmail dot com
Description:
------------
strtotime goes wrong for "last day of February next year"

"last day of February next year"  got the 31.03 the last day of march as result.

Test script:
---------------
$bug_from = 1406625924; // 29.07.2014 11:25
echo strftime('%d.%m.%Y %H:%M:%S', strtotime('last day of February next year', $bug_from));
echo ' != ';
echo strftime('%d.%m.%Y %H:%M:%S', strtotime('last day of February 2015', $bug_from));
die();

Expected result:
----------------
28.02.2015 00:00:00 != 28.02.2015 00:00:00

Actual result:
--------------
31.03.2015 00:00:00 != 28.02.2015 00:00:00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-29 09:48 UTC] greenrover33 at gmail dot com
I have seen this Report:
https://bugs.php.net/bug.php?id=50048

But different to them is, that i give a relative year.

Normaly it should translate the date first before it tries to resolve the day.
 [2014-07-29 10:39 UTC] derick@php.net
-Assigned To: +Assigned To: derick
 [2014-07-29 10:39 UTC] derick@php.net
You're right - this needs looking at.
 [2014-07-29 11:54 UTC] greenrover33 at gmail dot com
Workaround in PHP

$string = 'last day of february next year';
$matches = array();
if (preg_match('/ of (?<month_name>january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)\s*(?<year>(([0-9]{2,4})|((last|this|next|(\-[0-9]+)|(\+[0-9]+)) years?)?))?/i', $string, $matches)) {
    if (empty($matches['year'])) {
        $string .= ' ' . date('Y');
    } elseif (!is_numeric($matches['year'])) {
        if ($now > 0) {
            $string = str_replace($matches['year'], date('Y', strtotime($matches['year'], $now)), $string);
        } else {
            $string = str_replace($matches['year'], date('Y', strtotime($matches['year'])), $string);
        }
    }
}
 [2017-03-29 20:13 UTC] php-bugs at allenjb dot me dot uk
This issue appears to be fixed as of 5.6.8: https://3v4l.org/a1CVu
 [2017-03-30 07:34 UTC] requinix@php.net
-Status: Assigned +Status: Duplicate -Assigned To: derick +Assigned To:
 [2017-03-30 07:34 UTC] requinix@php.net
Long since fixed with bug #69336.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 02:01:29 2024 UTC