I am gettin fed up of date drop downs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

I am gettin fed up of date drop downs

Post by andylyon87 »

Hey guys

this is real easy but I can't find the code anywhere and cannot remember how to do it.

All I need is the php code to make a drop down (select) box for the days in a month

Thanks
andy
User avatar
pedrokas
Forum Commoner
Posts: 32
Joined: Thu Jan 15, 2004 10:53 am
Location: Lisboa, Portugal

Post by pedrokas »

<form name"blabla" method="" action"">
<select name="blabla">
<?

Code: Select all

for($i;$<32;$i++)
             &#123;
                 echo "<option>$i</option>";
             &#125;
?>
</select>
</form>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You have to be carefully with dates, as each month has a different number of days, not to mention leap years.

I would create an array like

$days = array('December' => 31, 'November' => 30');

and loop through each month getting its value, and then plug it into that for loop.
kenrbnsn
Forum Newbie
Posts: 13
Joined: Tue Jul 01, 2003 3:34 pm
Location: Hillsborough, NJ USA

Post by kenrbnsn »

Do you want a drop down for a particular month? The current month? All the months in the current year? Or something else?

Here's the code for the current month, with the current day already selected:

Code: Select all

<?php
$td = date('j',strtotime('today'));
echo '<form method=post>'."\n";
echo date('F: ',strtotime('today'));
echo '<select name=dayofmonth size=1><option value=" "> </option>'."\n";
for ($d=1;$d<date('t',strtotime('today'))+1;$d++) {
    echo '<option value='.$d;
    if ($d == $td) echo ' selected ';
    echo '>'.$d.'</option>'."\n"; }
echo '</select>'."\n";
echo '<input type=submit name=submit value="Select Day"></form>';
echo '<hr>';
if (isset($_POST['submit'])) {
   echo '<pre>';print_r($_POST);echo '</pre>'; }
?>
The other solutions are left as an exercise for the original poster. :)
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

thanks guys thats great, it makes things lots easier
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I use this: http://www.softcomplex.com/products/tigra_calendar/ .. Its free, flexible, and works everywhere.
kenrbnsn
Forum Newbie
Posts: 13
Joined: Tue Jul 01, 2003 3:34 pm
Location: Hillsborough, NJ USA

Post by kenrbnsn »

onion2k wrote:I use this: http://www.softcomplex.com/products/tigra_calendar/ .. Its free, flexible, and works everywhere.
And it's Javascript... :( So if someone who has Javascript turned off accesses you site, they can't see your calendar.

Also, how do you know the original poster wanted a full blown calendar program?

Ken
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

kenrbnsn wrote:And it's Javascript... :( So if someone who has Javascript turned off accesses you site, they can't see your calendar.
They'll still see the input box for the date though, so the application will work fine. Client-side things are only an issue if they break functionality of a site when the client hasn't got scripting switched on.

As for him not requiring a full-blown calendar.. fair enough. I said I use it. I didn't say he has to.
Post Reply