php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49115 date('M', strtotime('February')) prints "March"
Submitted: 2009-07-30 23:16 UTC Modified: 2009-07-31 14:58 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: php at davidstockton dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.10 OS: Windows XP/Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at davidstockton dot com
New email:
PHP Version: OS:

 

 [2009-07-30 23:16 UTC] php at davidstockton dot com
Description:
------------
If I use strtotime on 'February' the resulting timestamp is in March.

Reproduce code:
---------------
echo date('F', strtotime('February'));


Expected result:
----------------
February

Actual result:
--------------
March

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-30 23:25 UTC] php at davidstockton dot com
Also, not sure if this is related, but the following code has similar strange behavior:

for ($i = 1; $i <= 12; $i++) {
    echo date('F', gmmktime(0,0,0,$i)), "<br/>";
}

Output:
--------
January
March
March
April
May
June
July
August
September
October
November
December

Expected:
----------
January
February
March
April
May
June
July
August
September
October
November
December

I'd be happy to open a new defect if it's not related, or to be told why these are correct behavior.

Thanks.
 [2009-07-30 23:29 UTC] rasmus@php.net
date() is doing what it is supposed to.  You are passing it a timestamp in March.

strtotime() returns a timestamp in March because you have not been specific enough.  When you just give it a month and nothing else, it makes some assumptions.  For example, it takes the current year and the current date.  So, you are asking strtotime for February 30, 2009 which actually doesn't exist since February doesn't have 30 days, so it gives you the closest match which is March 2nd.

Not a bug.  You just have to be more explicit.  Like "February 1"
 [2009-07-31 02:57 UTC] php at davidstockton dot com
Interesting. So I'd guess that if I run this same test tomorrow, I'll be seeing

January
March
March
May
May
July
July
August
October
October
December
December

I think I understand why it would be doing this, but it still seems pretty weird.  Is the assumption that strtotime fills in every part of the date/time with whatever the current time happens to be correct?)
 [2009-07-31 03:55 UTC] php at davidstockton dot com
I guess my above assumption is not correct either which means there's something else going on that I don't understand.  For example,

<?php
echo date('F', strtotime('February 2009')), "\n";

echo date('F', strtotime('February 1')), "\n";
?>

Both lines output the expected "February" even though I've not specified a day of the month in the first scenario but the timestamp given by strtotime corresponds to February 1, 2009.  If the function fills in the defaults from the current date, I'd expect to still see March for the 1st line.)
 [2009-07-31 08:34 UTC] derick@php.net
Yup, that's correct. With the "month year" format, we reset the day to 1. 
 [2009-07-31 14:58 UTC] php at davidstockton dot com
Sorry to belabor the point, but why is "February 2009" reset to February 1, 2009 (since the day is not provided), but "February" is not?  In that case the current year is used (not provided) and the current day is used (also not provided).  In the February 2009 example the day is not provided, but the day is reset to 1 which I think is a reasonable adjustment.  I would think that February with an implied year matching the current year should behave the same as February with an implied year (no provided year).

Thank you again.)
 [2011-11-30 06:41 UTC] anto dot justus at gmail dot com
hi..

You have used date("M", mktime(0, 0, 0, $i))

In this issue there is no initialization of date so it took 30 as default initialize date it will check for the availability of the date, if it is available it will consider the month or it will not take month in account.

for example you can check the diff with using :
date("M", mktime(0, 0, 0, $i,[2011])); it will consider only the month which have 30 and more days - default [30]
date("M", mktime(0, 0, 0, $i,30,[2011])); it will consider only the month which have 30 and more days
date("M", mktime(0, 0, 0, $i,31,[2011])); it will consider only the month which have 31 and more days
date("M", mktime(0, 0, 0, $i,1,[2011])); it will consider only the month which have 1 and more days

The calendar has 12 months with 30 or 31 days except February. So february is exception in this case. ( february have less than 30 days)

The possible solution will be of using 1 as the 5th argument to initialize the month.

you need to make a not of initializing the month to 1, since 1st is common to all the 12 months.

you can use :
date("M", mktime(0, 0, 0, $i,1,[2011]));

year is optional it will take current year as default.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 17:01:30 2024 UTC