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


Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.