Jump to content

[SOLVED] Making a drop down box with years


mazman13

Recommended Posts

I have to throw in my two cents since this something I've been doing for awhile

<?php
$current = date("Y", strtotime('-20 years'));
$future = date("Y", strtotime('+10 years'));
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$years = range ($current, $future);
?>

 

Then, to call them in a drop-down

<?php
echo "Year: <select name='year'>";
foreach ($years as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select><br />';

echo "Day: <select name='day'>";
foreach ($days as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select><br />';

echo "Month: <select name='month'>";
foreach ($months as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select><br />';
?> 
[/code


Just for my 2 pence

For getting months and days i hate typing the jan and feb over and over i use

 

for($i=1;$i<=12;$i++){$month[$i]=date("F",mktime(0,0,0,$i+1,0,0));} gives $month[1]=january

for($i=1;$i<=7;$i++){$days[$i]=date("l",mktime(0,0,0,0,$i+5,0));} gives $days[1] = monday

 

hopefuilly some use to some1. :)

Ok with the foreach one, how to i make it so that the current year shows first? It prob needs to be in desc order.

 

echo "<select name='year'>";
foreach (range(date('Y')-20, date('Y')) as $year) {
  echo "  <option value='$year'>$year</option>";
}
echo "</select>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.