ID: 22957 Updated by: [EMAIL PROTECTED] Reported By: jacques dot daguerre at st dot com Status: Bogus Bug Type: Date/time related Operating System: Linux RedHat 6.2/7.3 PHP Version: 4.3.1 New Comment:
This behaviour is already fully documented at http://www.php.net/manual/en/function.mktime.php, which also gives a one-liner for finding the last day of any month (see Example 2). Previous Comments: ------------------------------------------------------------------------ [2003-03-31 05:16:32] jacques dot daguerre at st dot com 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. ------------------------------------------------------------------------ [2003-03-31 04:27:00] jacques dot daguerre at st dot com for the comment of [EMAIL PROTECTED] $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. ------------------------------------------------------------------------ [2003-03-31 01:14:14] [EMAIL PROTECTED] Using date('m') and date('d') are wrong as those have the leading zeros in them. Not bug in PHP. ------------------------------------------------------------------------ [2003-03-30 05:59:35] noel at crewe-it-nosp dot co dot uk I've also hit the problem with mktime giving incorrect results. On 4.3.2-dev it atually returns -3662 as the date value. Worse though, it returns the same value for 31st March 2002 28th March 2004 27th March 2005 26th March 2006 and so on. ------------------------------------------------------------------------ [2003-03-30 03:25:45] 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 ! ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=22957&edit=1