tags:

views:

43

answers:

2

Hello SO! I am working on a small project and am playing with date() and mktime() in PHP. Compare the two code blocks and their output, notice the second sample adds one to the month in it's first mktime.

$monthis = 5;
echo date('F', mktime(0,0,0,$monthis,0,0)) . " 1, 2010 is on a " . date("l F", mktime(0, 0, 0, $monthis, 1, 2010));

puts out

April 1, 2010 is on a Saturday May

but if I change it to

$monthis = 5;
echo date('F', mktime(0,0,0,$monthis + 1,0,0)) . " 1, 2010 is on a " . date("l F", mktime(0, 0, 0, $monthis, 1, 2010));

puts out

May 1, 2010 is on a Saturday May

Why do I have to add one to the month in the first mktime so that both emit the same month?

Any help or clarity would be appreciated. Thanks :)

+1  A: 

Because you are setting everything else to 0. Make the seconds/hour/minute be 1 or something.

webdestroya
A: 

When you do this:

mktime(0,0,0,$monthis,0,0)

You are setting the day to 0. mktime will think you want the day before the first of May (which would be the last day of April). Set the day to 1 (or don't pass a day, year at all) and it will return May.

webbiedave
I discovered this shortly before I checked back. Thank you.
mjboggess