|
Posted by Rik Wasmus on 01/02/08 19:01
On Wed, 02 Jan 2008 19:09:41 +0100, Steve <no.one@example.com> wrote:
>
> "ashore" <shoreas@gmail.com> wrote in message
> news:d6ba01a0-1681-4377-a6d1-3d75297b7b59@s12g2000prg.googlegroups.com=
....
>> Guys, the line below just returned "Dec 07" as the date for one month=
>> back from today. Hardly life-threatening, but any thoughts?
>>
>> <?php print date("M `y", mktime(0, 0, 0, date("m")-1, date("d"),
>> date("Y")));?>
>
> it's a known bug.
>
> php uses approximation methods to arrive at a date. most of the time i=
t
> works. essentially, php assumes an average of 30 days in every month. =
you
> can use 12/30/2007 23:59:59 and subtract one month from it and get Nov=
> `07...
This statement is utter nonsense. Why waste a perfectly good allready =
given explanation with garbage Steve? For your pleasure, the proof (than=
k =
god for february, be carefull about leap years..):
<?php
date_default_timezone_set('Europe/Amsterdam');
$date =3D mktime(12,0,0,3,1,2007);//1 march 2008
print date("M `y", mktime(0, 0, 0, date("m",$date)-1, date("d",$date), =
date("Y",$date)));
?>
According to your 'approximation method', this should return january, as=
=
31-01-2007 is 30 days before the 1st of march. It does not. The real =
answer is still: 'if in mktime the nth day of a month doesn't exist, PHP=
=
will assume you mean the (n-<days in month>)th of the next month'.(Or in=
=
case of negative numbers, the (n+<days in month>)th of the previous mont=
h. =
Similarly:
<?php
date_default_timezone_set('Europe/Amsterdam');
//march the 63 is assumed to be may 2nd
print date("d M `y", mktime(0, 0, 0, 3, 63, 2007));
//february the 0th is assumed to be january the 31st
print date("d M `y", mktime(0, 0, 0, 2, 0, 2007));
//april the -1th is assumed to be march the 30th
print date("d M `y", mktime(0, 0, 0, 4, -1, 2007));
//the 0th month is assumed to be december
print date("d M `y", mktime(0, 0, 0, 0, 1, 2007));
//the -1th month is assumed to be november
print date("d M `y", mktime(0, 0, 0, -1, 1, 2007));
?>
If you have imagined an answer to a question, please be sure it's the =
right one before spreading it around.
> i tend to avoid this problem by getting the first of the month and the=
n
> doing calculations from that (in similar contexts).
Which was allready suggested.
-- =
Rik Wasmus
[Back to original message]
|