php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62194 Bug on using strtotime to date
Submitted: 2012-05-31 05:49 UTC Modified: 2012-05-31 11:24 UTC
From: kg_emopunk at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.13 OS: Linux/ Windows
Private report: No CVE-ID: None
 [2012-05-31 05:49 UTC] kg_emopunk at yahoo dot com
Description:
------------
---
From manual page: http://www.php.net/function.strtotime
---

There is a slight bug using strtotime to date.
When the strtotime parameter is only Month, the output adding date syntax is 
incorrect.

Incorrect month outputs:
 Feb / February
 Apr / April
 Jun / June
 Sep / September
 Nov / November

The solution for this is to add a day or a year. 
e.g ( date("F",strtotime($month." ".$day)); )


Test script:
---------------
<?php
$s=array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$s1=array('January','February','March','April','May','June','July','August','September','October','November','December');

echo "<h4> 3 Letters Months </h4>";
foreach ($s as $key) {
  echo date("F",strtotime($key))."<br>";
}
echo "<h4> Full Text Months </h4>";
foreach ($s1 as $key) {
  echo date("F",strtotime($key))."<br>";
}
?>

Expected result:
----------------
3 Letters Months

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

Full Text Months

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

Actual result:
--------------
3 Letters Months

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-31 11:24 UTC] rasmus@php.net
There isn't really a bug here. If you try this same script tomorrow you will get 
your expected output. strtotime() will use the current date for any missing 
info, so since today is the 31st of the month, it will try to get the 31st of 
each month. Any month that doesn't have 31 days will end up giving you the 
appropriate offset into the following month. This is consistent with relative 
date offsets and related to the standard Unix approach to handling impossible 
dates described here:

http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-
strings.html#SEC125

The real solution is to specify a day in the month if you want your code to work 
reliably. As in strtotime("$key 1")
 [2012-05-31 11:24 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 16:01:31 2024 UTC