Click to See Complete Forum and Search --> : can I get the month name of a number?


Bobby_S
07-14-2005, 04:52 PM
Is this possible in PHP?
I want to create a for-loop for this: (but how to?)

if ($selected == '1')
print "<option value=\"1\" SELECTED>January</option>\n";
else print "<option value=\"1\">January</option>\n";
if ($selected == '2')
print "<option value=\"2\" SELECTED>February</option>\n";
else print "<option value=\"2\">February</option>\n";
if ($selected == '3')
print "<option value=\"3\" SELECTED>March</option>\n";
else print "<option value=\"3\">March</option>\n";
if ($selected == '4')
print "<option value=\"4\" SELECTED>April</option>\n";
else print "<option value=\"4\">April</option>\n";
if ($selected == '5')
print "<option value=\"5\" SELECTED>May</option>\n";
else print "<option value=\"5\">May</option>\n";
if ($selected == '6')
print "<option value=\"6\" SELECTED>June</option>\n";
else print "<option value=\"6\">June</option>\n";
if ($selected == '7')
print "<option value=\"7\" SELECTED>July</option>\n";
else print "<option value=\"7\">July</option>\n";
if ($selected == '8')
print "<option value=\"8\" SELECTED>August</option>\n";
else print "<option value=\"8\">August</option>\n";
if ($selected == '9')
print "<option value=\"9\" SELECTED>September</option>\n";
else print "<option value=\"9\">September</option>\n";
if ($selected == '10')
print "<option value=\"10\" SELECTED>October</option>\n";
else print "<option value=\"10\">October</option>\n";
if ($selected == '11')
print "<option value=\"11\" SELECTED>November</option>\n";
else print "<option value=\"11\">November</option>\n";
if ($selected == '12')
print "<option value=\"12\" SELECTED>December</option>\n";
else print "<option value=\"12\">December</option>\n";


thx!

NogDog
07-14-2005, 08:53 PM
$monthName = date('F', mktime(0,0,0,$selected));

Bobby_S
07-15-2005, 07:56 AM
Geee, I would never have found this myself :p
Thanks for your help, NogDog!

NogDog
07-15-2005, 02:23 PM
You're welcome. :)

Juuitchan
07-15-2005, 02:41 PM
I think your code is buggy, NogDog.

What happens if you try it when your system clock (assuming it is your system the PHP is running on) thinks it is the 31st of some month (say, July) and you tell PHP to print out the name of month #2 using your code? Will you get "March" (as there is no Feb. 31 in any year)? This is how PHP works, isn't it? Or am I wrong?

Perhaps your code can be fixed by specifying a day (say, the 15th) of the month.

. . . . . .

Bobby_S, why did you not simply populate an array with month names, and then refer to this array when you wanted the name of a month? If I remember my PHP, you could make a string with the month names, separated by dashes, and then use the explode() function to make an array from it. If PHP uses zero-based array indexing, though, begin the array with a fake word (not a month name) so that the months line up with the right numbers.

NogDog
07-15-2005, 02:48 PM
Good point, we should add the day of the month to mktime, I guess:

$monthName = date('F', mktime(0,0,0,$selected,1));

(The peer review process works again. :) )