php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22957 Date output is not correct
Submitted: 2003-03-30 03:25 UTC Modified: 2003-03-31 09:47 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jacques dot daguerre at st dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 4.3.1 OS: Linux RedHat 6.2/7.3
Private report: No CVE-ID: None
 [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 !

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-30 05:59 UTC] 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-31 01:14 UTC] sniper@php.net
Using date('m') and date('d') are wrong as those have the leading zeros in them. Not bug in PHP.


 [2003-03-31 04:27 UTC] jacques dot daguerre at st dot com
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.
 [2003-03-31 05:16 UTC] 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 05:43 UTC] mgf@php.net
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).
 [2003-03-31 09:25 UTC] rasmus@php.net
There is no bug here.  You are asking for the timestamp for February 31st.  What is it supposed to do?  Read your mind and figure out you want the last day of February?  That's impossible.  It does the only sensible thing and gives you the actual date 31 days beyond Feb.1.  If you want the last day of a month, get the 1st of the following month and subtract a day.
 [2003-03-31 09:43 UTC] jacques dot daguerre at st dot com
The case is closed
 [2003-03-31 09:44 UTC] rasmus@php.net
"Closed" means we fixed something.  There was never anything to fix here, hence Bogus.
 [2003-03-31 09:47 UTC] jacques dot daguerre at st dot com
Could you please stop adding to this !
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 10:01:32 2024 UTC