Forum Moderators: coopster

Message Too Old, No Replies

Months and date()

         

iceman22

2:11 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



I encountered a small problem with date functions. It appears that when minusing a month from the date, 31 days is subtracted, regardless of whether the month has 30 or 31 days. Strange thing is that the second example here uses the mktime format, exactly the same as in the PHP manual.

$date = array(date("d.m.y",strtotime("-6 month")),
date("d.m.y",strtotime("-5 month")),
date("d.m.y",strtotime("-4 month")),
date("d.m.y",strtotime("-3 month")),
date("d.m.y",strtotime("-2 month")),
date("d.m.y",strtotime("-1 month")),
date("d.m.y"));

and

$date = array(date("d.m.y",mktime(0,0,0,date("m")-6,date("d"),date("Y"))),
date("d.m.y",mktime(0,0,0,date("m")-5,date("d"),date("Y"))),
date("d.m.y",mktime(0,0,0,date("m")-4,date("d"),date("Y"))),
date("d.m.y",mktime(0,0,0,date("m")-3,date("d"),date("Y"))),
date("d.m.y",mktime(0,0,0,date("m")-2,date("d"),date("Y"))),
date("d.m.y",mktime(0,0,0,date("m")-1,date("d"),date("Y"))),
date("d.m.y",mktime(0,0,0,date("m"),date("d"),date("Y"))));

will output this:

Array
(
[0] => 31.07.04
[1] => 31.08.04
[2] => 01.10.04
[3] => 31.10.04
[4] => 01.12.04
[5] => 31.12.04
[6] => 31.01.05
)

also, this function

echo date("d.m.y",mktime(0,0,0,1,0,2005));

will output this:

31.12.04

I wasn't expecting these results, I could retool the code checking the days of the month using date("t") but it's not a very good solution.

I also have a script that will go through a text file, and it will start writing when it finds a matching date, then stop writing from the file when it encounters a date that isn't a matching date.

The current code I've got works quite well, but I sometimes get a false match for a date if there is an odd combination of numbers and characters. Is it possible to recognise a date only if it is in a specific format, date("l dS of F Y H:i:s") for example.

Many thanks.

dcrombie

3:17 pm on Jan 31, 2005 (gmt 0)



The date system works so that the "last" day of each month is also the "zeroth" day of the next month.

That also means that the "31st" of a month with 30 days will be the "1st" of the next month (or in the case of February either the "2nd" or "3rd" of Match).

Hope that helps ;)

iceman22

4:35 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



Thanks, now I understand why it's doing that, but I'm not sure what I should do.

I don't use the date, only the month and the year, I'll specify a value of one for the day to keep the month like this:

date("M Y",mktime(0,0,0,date("m")-5,1,date("Y")));

Question is, what if you need the date? The date("m") returns the month, the date("Y"), returns the year, why not the date("d"), am I missing something?

dcrombie

4:52 pm on Jan 31, 2005 (gmt 0)



You're looking at it from the wrong angle. What do you actually want returned in each case?

iceman22

5:22 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



Well it's way past my bed time, but I just want the month and the year, before I was getting two Decembers, etc. because of the of the 1st and 31st results. The script works properly now, I replaced all the date("d") occurances with 1.

Now I just need to figure out the second problem...

Still not sure why date("d") returns 0, then shouldn't date("Y") return 0 as in 1970?