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: 2014-07-29 11:54 UTC
Votes:9
Avg. Score:3.3 ± 1.4
Reproduced:7 of 9 (77.8%)
Same Version:3 (42.9%)
Same OS:4 (57.1%)
From: greenrover33 at gmail dot com Assigned: derick
Status: Assigned Package: Date/time related
PHP Version: 5.6Git-2014-07-29 (Git) OS: Linux/Independent
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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);
        }
    }
}
 
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Tue Mar 03 11:02:10 2015 UTC