By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Last Day of Year Date Bug?

P: n/a
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")));?>

AS
Dec 31 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
ashore wrote:
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")));?>

AS
Interesting. Looks like it may be a bug.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 31 '07 #2

P: n/a
On Mon, 31 Dec 2007 14:18:34 +0100, Jerry Stuckle
<js*******@attglobal.netwrote:
ashore wrote:
>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")));?>
AS

Interesting. Looks like it may be a bug.
Nope,
date('m')-1 = 11
date('d') = 31

31-11-2007 = 01-12-2007 as far as php is concerned.

Not 'end of a year' bug, but '31st of every month' bug (well, feature I'd
say, I know what to expect), with a few bonus dates in march.
--
Rik Wasmus
Dec 31 '07 #3

P: n/a
Rik Wasmus wrote:
On Mon, 31 Dec 2007 14:18:34 +0100, Jerry Stuckle
<js*******@attglobal.netwrote:
>ashore wrote:
>>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")));?>
AS

Interesting. Looks like it may be a bug.

Nope,
date('m')-1 = 11
date('d') = 31

31-11-2007 = 01-12-2007 as far as php is concerned.

Not 'end of a year' bug, but '31st of every month' bug (well, feature
I'd say, I know what to expect), with a few bonus dates in march.
Ah, yes. Now I remember why I don't do it this way - it's been so long
I forgot.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 31 '07 #4

P: n/a
Feature or bug, that's pretty bad (where 'bad' == 'non-intuitive').

While correcting this will prbly break a lot of applications, mebbe
it's time for another date set of functions.

What DO you guys use to 'go back one month' reliably?

-AS
Dec 31 '07 #5

P: n/a
On Mon, 31 Dec 2007 17:28:11 +0100, ashore <sh*****@gmail.comwrote:
Feature or bug, that's pretty bad (where 'bad' == 'non-intuitive')..
Well, it's something I rather depend on most of the time actually. How do
you propose it should handle '31-11-XXXX'? An error/exception perhaps,
that would be justifiable. Just setting it back to 30-11 would be
inexcusable.
While correcting this will prbly break a lot of applications, mebbe
it's time for another date set of functions.

What DO you guys use to 'go back one month' reliably?
Depends on the use. In some cases, taking 01-12-2007 as one month before
31-12-2007 is just what I want. In your context, why do you bother with
the day part?

print date("M `y", mktime(0, 0, 0, date("m")-1, 1,date("Y")));
--
Rik Wasmus
Dec 31 '07 #6

P: n/a
In article
<06**********************************@l6g2000prm.g ooglegroups.com>,
ashore <sh*****@gmail.comwrote:
Feature or bug, that's pretty bad (where 'bad' == 'non-intuitive').

While correcting this will prbly break a lot of applications, mebbe
it's time for another date set of functions.

What DO you guys use to 'go back one month' reliably?
First we should ask - what does your question mean?

Does "go back one month" mean go back 31 days or does it mean "go to
same numbered day of previous month"?

Each definition has its problems. 2nd March minus 31 days takes you to
January. From 31st Dec, there *is no* same numbered day in previous
month.
Dec 31 '07 #7

P: n/a
On Dec 31, 8:28 am, ashore <shor...@gmail.comwrote:
Feature or bug, that's pretty bad (where 'bad' == 'non-intuitive').

While correcting this will prbly break a lot of applications, mebbe
it's time for another date set of functions.

What DO you guys use to 'go back one month' reliably?

-AS
I use 1 as the day, if I want to find the last month, or a simple
$lastmonth = ($curmonth == 1,12, $curmonth -1);

The date with mktime is great for calculating out an arbitrary date
such as add 30 days to the current date, etc. It's not a bug, but a
great feature, if you understand how it works.

Here is some date arithmetic I posted that uses mktime, just to give
you an idea.

http://groups.google.com/group/comp....d94e81f2ec396a

Jan 1 '08 #8

P: n/a

"ashore" <sh*****@gmail.comwrote in message
news:d6**********************************@s12g2000 prg.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
Jan 2 '08 #9

P: n/a
On Wed, 02 Jan 2008 19:09:41 +0100, Steve <no****@example.comwrote:
>
"ashore" <sh*****@gmail.comwrote in message
news:d6**********************************@s12g2000 prg.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...
This statement is utter nonsense. Why waste a perfectly good allready
given explanation with garbage Steve? For your pleasure, the proof (thank
god for february, be carefull about leap years..):

<?php
date_default_timezone_set('Europe/Amsterdam');
$date = 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 month.
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 then
doing calculations from that (in similar contexts).
Which was allready suggested.
--
Rik Wasmus
Jan 2 '08 #10

P: n/a
..oO(Steve)
>"ashore" <sh*****@gmail.comwrote in message
news:d6**********************************@s12g200 0prg.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.
Wrong.
>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.
Wrong again. PHP knows exactly how many days each month has, it just
uses its own way to fix invalid dates like YYYY-11-31 for example.
>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
2007-12-30 - 1m = 2007-11-30
>// now, watch this...
$date = strtotime('+1 second', $date);
// you guessed it...Dec `07
2007-12-31 - 1m = 2007-11-31 = 2007-12-1

So where's the problem? PHP's date handling is described in detail in
the manual. It works as intended.

Micha
Jan 2 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.