|
Posted by Steve on 01/02/08 18:09
"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 it
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...
$date = strtotime('12/30/2007 23:59:59');
// echo date('M `y', $date); // Nov `07
// now, watch this...
$date = strtotime('+1 second', $date);
// you guessed it...Dec `07
i tend to avoid this problem by getting the first of the month and then
doing calculations from that (in similar contexts). php tends to correct the
30 day averaging bug when using the first of any month...at least i've never
had a problem like this when using the following:
$date = strtotime('12/31/2007 23:59:59');
$date = strtotime(date('m', $date) . '/01/' . date('Y', $date));
echo date('M `y', strtotime('-1 month', $date));
hth,
me
Navigation:
[Reply to this message]
|