I am writing a project with considerable date handling in PHP. In doing so, I wrote a piece of code that was consistently malfunctioning, making use of PHP's date()
function. I have isolated the code (the rest of the code in the project is irrelevant), and here it is:
<?php
date_default_timezone_set("GMT");
echo date("F m, Y g:ia",strtotime("April 15, 2012 10:00am"));
//Output: April 04, 2012 10:00am
//Should be: April 15, 2012 10:00am
?>
In theory (I think), this code should calculate the timestamp of April 15, 2012, at 10:00 in the morning. This seems to be happening correctly. The date()
function should then turn that back into a human-readable date in the same format that was input. It doesn't, though. It outputs April 04, 2012 10:00am
. In May, it says May 5th; in June, June 6th. So I think there is some bug that makes it confuse the day of the month with the month itself.
On the other hand, it could be some weird problem that I didn't ever consider. I'm looking for second opinions/"you're so stupid you're doing X wrong"s. If it is a bug, I'll report it. I have trouble believing it's a bug, since with these dates coming up so soon I would think that it would have been noticed by now.
http://php.net/manual/en/function.date.php
No, you've just messed up your date format string - F m
means "Month (textual) Month (numeric) - it's printing 04 because that's the month number for April.
You want a j
instead to print the day of the month:
echo date("F j, Y g:ia",strtotime("April 15, 2012 10:00am")); // prints "April 15, 2012 10:00am" as expected.
Any time you think you've detected a bug in the tools take a VERY long look at your code!