473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Last Day of Year Date Bug?

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
10 3248
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
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
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
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
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
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
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

"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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John | last post by:
If anyone can help me out with a good way to do this in javascript I would greatly appreciate it. I need to compute three dates in javascript - all of which relate to a given date, say todays...
3
by: Melissa | last post by:
I have this table: TblProjectYear ProjectYearID ProjectYearStartDate ProjectYearEndDate The Project Year will always span across December 31; for example 9/1/04 to 6/30/05. How do I build a...
7
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date....
0
by: Lee Harr | last post by:
I wrote a function to return the first date of a given week (and a few related functions) : -- return the first date in the given week CREATE or REPLACE FUNCTION week_start(integer, integer)...
5
by: rjfjohnson | last post by:
Hey, Today is Thursday 16-Feb-06. The same thursday last year is 17-Feb-05. Because I am comparing daily sales between years, I need to know the date of the same weekdayname as last year, so...
4
by: laredotornado | last post by:
Hi, Using PHP 4, if I have a date, what is a function I could use to give me a date that represents the first day of that month? For example, if my date were "3/19/2006 8:00", I would want my...
6
by: phforum | last post by:
If user input the date is 2006-07-01. How to convert it to last year 2005-07-01? Thanks
1
by: Svetac | last post by:
Hi, I use a script that shows when the page was last modified. It works fine for just one html file. The thing I would like to do is that the script shows me of last modified file in root...
3
by: ats | last post by:
Does anybody have any sample code for calculating the date for teh last Friday in each month. TIA -- ats@jbex When an old lady got hit by a truck I saw the wicked gleam in your eyes
0
by: marlberg | last post by:
Platform: Windows2000, WindowsXP, Windows Vista, etc Language: C#, ASP.NET Pre-compiled Libraries: Enterprise Library 3.0 full I have a requirement to implement in and display in C# and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.