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:

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.


Previous Comments:
------------------------------------------------------------------------

[2003-03-31 05:43:38] [EMAIL PROTECTED]

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 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.

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/22957

-- 
Edit this bug report at http://bugs.php.net/?id=22957&edit=1

Reply via email to