Re: [PHP] Date Problem - Last Day Of Month
- 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. > > > > > for ($i=1; $i<=12; $i++) { > if ($i == date("m")) { > print "" . > date("F",mktime(0,0,0,$i,date("d"),date("Y"))) . ""; > } else { > print "" . > date("F",mktime(0,0,0,$i,date("d"),date("Y"))) . ""; > } > } > > ?> > > > > 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 "\n"; } HTH, Kevin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date Problem - Last Day Of Month
change for ($i=1; $i<=12; $i++) to for ($i=1; $i<12; $i++) --- Vinesh Hansjee <[EMAIL PROTECTED]> wrote: > 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. > > > > > for ($i=1; $i<=12; $i++) { > if ($i == date("m")) { > print "" . > date("F",mktime(0,0,0,$i,date("d"),date("Y"))) . > ""; > } else { > print "" . > date("F",mktime(0,0,0,$i,date("d"),date("Y"))) . > ""; > } > } > > ?> > > > > Thanks > Vinesh > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > __ Do you Yahoo!? Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop! http://platinum.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [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... I'm not sure what you mean by this, but I can be a moron on this list sometimes and the clear answer usually comes to me about 2 seconds after I hit the send button. Do you mean that you don't want any months in the SELECT control when it's the last of the month? Or you don't want the SELECTED attribute in the OPTION tags? As far as I can tell, you're not repeating any months, but you're only listing them once. Try this (and IMHO a *slight* *slight* improvement in clarity, but again, that's my HO): " . date("F",mktime(0,0,0,$counter,date("d"),date("Y"))) . ""; } } I adjusted it so that you only print out the SELECTED attribute if it's the current month, and I called $i as $counter, because, to me anyway, it makes it clearer what is the $counter. If $i works for you, though, there's nothing wrong with that, and it's not that it's unclear anyway, what with it in the FOR loop only a few lines earlier. (I'll shut up now about $counter.) Anyway, I added a comparison above that said if the current date is less than the last day of the month, then print SELECTED. That's assuming I have your question right in that you don't want to select the current month if it's the last day of that month. Otherwise, if I'm wrong, you can take that comparison and put it just about anywhere. Help? Doesn't? Am I a complete goof? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php