Re: Date Problem - Last Day Of Month

From: Date: Mon, 31 Mar 2003 18:57:56 +0000
Subject: Re: Date Problem - Last Day Of Month
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-141720@lists.php.net to get a copy of this message
Or just make the date("d") a 1. Every month has a day #1 --- Kevin Stone <kevin@helpelf.com> wrote: > > ----- Original Message ----- > From: "Vinesh Hansjee" <vins@vins.co.za> > To: <php-general@lists.php.net> > 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 > __________________________________________________ Do you Yahoo!? Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop! http://platinum.yahoo.com

« previous php.general (#141720) next »