|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-03-30 03:25 UTC] jacques dot daguerre at st dot com
PHP Bug with mktime ???..
I just checked with 2 different versions of PHP :
PHP 4.1.2 and PHP 4.2.1...
Sorry I have not installed the latest version but I could not find anything in the changelog either !..
The following code TODAY (only today March 30th, 2003) is not giving the expected output .
$lastmonth1 = mktime (0,0,0,(date("m")-1),date("d"),date("Y"));
$lastmonth = date ("Y-m-d", $lastmonth1);
The result of lastmonth should show "2003-02-28" and it shows "2003-03-02"..
Looks like a bug to me !
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Fri Feb 13 03:00:01 2026 UTC |
Using date('m') and date('d') are wrong as those have the leading zeros in them. Not bug in PHP.for the comment of sniper@php.net $lastmonth1 = mktime (0,0,0,(date('n')-1),date('j'),date('Y')); $lastmonth2 = mktime (0,0,0,(date('m')-1),date('d'),date('Y')); $lastmonth1 and $lastmonth2 are both set to the same value : 1046646000 for today Marh 31st, 2003. The (date('m')-1) calculation works and mktime() doesn't seem to be taking the leading 0's into consideration.It was probably not a bug after all and I probably had my code bugged for many months without noticing it until yesterday... I probably misunderstood the way mktime works. If you a day that is greater than the maximum day of the month, then mktime will go for the following month: a date like : mktime (0,0,0,2,31,2003) will be March 03, 2003 even the month entered is February. Since Feb 31 doesn't exist it will consider the (31-28)th day in the following month, and therefore the result of March 03. The calculation of a date a month ago cannot just simply be : $lastmonth1 = mktime (0,0,0,(date('m')-1),date('d'),date('Y')); I corrected my code to be : $now1 = mktime (0,0,0,date("m"),date("d"),date("Y")); $now = date ("Y-m-d", $now1); $today_day= date ("d", $now1); $lastmthday = mktime (0,0,0,date('m'),0,date('Y')); $lastday = date ("d", $lastmthday); if ( $today_day > $lastday) {$prevd = $lastday; }else{$prevd = $today_day;} $lastmonth1 = mktime (0,0,0,(date('m')-1),$prevd,date('Y')); $lastmonth = date ("Y-m-d", $lastmonth1); this will calculate also the last day of the previous month and make the day date of the day will not be a higher number than the last of the previous month. This works fine. I would suggest to post this on the mktime function page as I guess other people could make the mistake as well.