Jump to content

Bizarre "for loop" behavior


cadams7407

Recommended Posts

hello world ... haha

 

anyways. I have a for loop that I use to generate a list of months. Its worked very well .. until today (2007-01-31)

 

heres code:

echo "<TABLE BORDER=0 CELLPADDING=3 style=\"BORDER-COLLAPSE: collapse\"><tr><td align=right bgcolor='#F4ECA5'><B>";
//This generates the months Jan - Dec and lists them down the left of the table
for($m = 01;$m <= 12; $m++){
$month = date("F", mktime(0, 0, 0, $m));
echo "<option value='$m'>".$month."<br /></option>";
}
echo "</B></td><td>";

 

Normally I get a nice list like:

January

February

March

etc.

 

But today the list is:

January

March

March

May

May

July

July

August

October

October

December

December

???

 

Wether or not this changes tomorrow (2007-02-01) will give me a clear indication if this is a php bug, or if my code is too sloppy.

 

In the mean time, any advice out there?

 

Thank you in advance.

 

Almost forgot: php 4.3.11 | MySQL 4.1.11 | phpMyAdmin 2.6.4 | Apache 2.0.58 | Zend Opt. 2.6.2

Link to comment
Share on other sites

That is pretty funky...Uhm, can you try changing the 01 to just 1?

 

Whenever I need to do this, I just use a simple array

$months[1] = 'January'; etc, and just do foreach on it. It sounds faster than making timestamps and using date. Perhaps try that?

Link to comment
Share on other sites

Per the manual for mktime:

 

Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.

 

You did not give mktime a day or year, so it is using 31 / 2007. There is no February 31st, April 31st, June 31st, September 31st, or Novermber 31st this year, so they are spilling over into the next month.

 

Use a day that every month has: mktime(0, 0, 0, $m, 1)

Link to comment
Share on other sites

Per the manual for mktime:

 

Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.

 

You did not give mktime a day or year, so it is using 31 / 2007. There is no February 31st, April 31st, June 31st, September 31st, or Novermber 31st this year, so they are spilling over into the next month.

 

Use a day that every month has: mktime(0, 0, 0, $m, 1)

 

right on, makes sense.

 

its working fine today .. but todays the 1st .. so ... yeah, what you said.

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.