php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43739 Handling of adding days to date incorrect
Submitted: 2008-01-03 16:42 UTC Modified: 2008-01-03 19:18 UTC
From: phreaks at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.5 OS: Linux
Private report: No CVE-ID: None
 [2008-01-03 16:42 UTC] phreaks at gmail dot com
Description:
------------
When adding days to a date returns wrong weekday.
This only happens when adding days to 12-31-07.
Workaround is handle any new year dates by starting from 1-1-08 and adding days.

Reproduce code:
---------------
$fm = 12;
$day = 31;

for ($i = 0; $i < 7; $i++)
 echo date("M-d (w)", mktime(1,1,1,$fm,$day++))."<BR>";

Expected result:
----------------
Expected output
Dec-31 (1)
Jan-01 (2)
Jan-02 (3)
Jan-03 (4)
Jan-04 (5)
Jan-05 (6)
Jan-06 (0)

Actual result:
--------------
The output of above is:
Dec-31 (3)
Jan-01 (4)
Jan-02 (5)
Jan-03 (6)
Jan-04 (0)
Jan-05 (1)
Jan-06 (2)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-03 19:18 UTC] derick@php.net
There is no bug here:

$fm = 12;
$day = 31;
echo date("M-d (w)", mktime(1,1,1,$fm,$day)), "\n";

shows correctly:
Dec-31 (3)

You don;t specify the year to mktime, so it assumes the current one, which is now 2008. Your output is correct for the year 2007. Change your code to this to see it:

$fm = 12;
$day = 31;

for ($i = 0; $i < 7; $i++)
 echo date("M-d Y (w)", mktime(1,1,1,$fm,$day++))."\n";
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Fri Jan 09 14:00:01 2026 UTC