----- Original Message -----
From: "Vinesh Hansjee" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, March 31, 2003 6:55 AM
Subject: [PHP] Date Problem - Last Day Of Month


> Hi there, can anyone tell me how to fix my code so that on the last day of
> the month, my code doesn't repeat the months...
> What the code suppose to do is, makes a drop down box from which you can
> select the month, and if its the current month
> the box is already selected.
>
> <select name="month">
>
> <?
>
> for ($i=1; $i<=12; $i++) {
>  if ($i == date("m")) {
>   print "<option selected value=\"". $i . "\">" .
> date("F",mktime(0,0,0,$i,date("d"),date("Y"))) . "</option>";
>  } else {
>   print "<option value=\"". $i . "\">" .
> date("F",mktime(0,0,0,$i,date("d"),date("Y"))) . "</option>";
>  }
> }
>
> ?>
>
> </select>
>
> Thanks
> Vinesh


Vinesh, there's a fundemental flaw in your logic.  The date("d") input will
fail to produce a reliable list if it is executed on any day after the 28th
of the month.  Since today is the 31st obviously this is going to cause some
problems becuase any month that ends on the 30th or earlier will return plus
one the month.  So February will return March, April willl return May, and
so on.  If you wait until tommorrow (the 1st) your function will magically
work.  :)

One way to fix this is to build your own array of month names and reference
that instead of using the date() function.

$months = array(1=>"January", 2=>"February", 3=>"March", 4=>"April",
5="May", 6=> ... and so on...);

for($i=1; $i<=12; $i++)
{
    $selected = "";
    if ($i == date("m"));{$selected = "SELECTED";}
    echo "<option $selected value=\"${months[$i]}\">\n";
}

HTH,
Kevin




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to