PHP Date Drop-Down Menu Function
If you have ever had to deal with long hours coding large drop-down menus for various different reasons, then you know finding a better solution isn’t far after those first 20 lines. When programming a scheduling application last year, I realized that I didn’t want to rely on an employee to know the date format for the database, or even what the current day was. Instead of coding lines upon lines of “select” boxes, and hoping that people would soon learn these simple facts, I decided to create a PHP function to create a dummy-proof Date/Time drop-down menu. Feel free to use this all you want, just always remember where you obtained this code.
Setting Up Our PHP Date Drop-Down Script
Let’s quickly drop this into a php file, so we can make use of it in our app:
//datetime.php by Dennis Brown,
//2007 Network Synapse LLC
class menus
{
function date_menu()
{
// Get the Current Date and Time from the system itself
$date = date("Y-m-d");
$time = date("H:i");
// Cut the time up into bits. This is here so that if you wanted to have $date and $time pulled from a database
// you could have it parse those dates and times as well.
list ($year, $month, $day) = split('-', $date);
list ($hour, $min) = split(':', $time);
// Create our number arrays
$month_array = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$num_array = array('00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13',
'14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31');
$min_array = array('00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50',
'55');
// The Year drop-down menu
echo "<select name=year>";
for($num = 2006; $num < 2015; $num++)
{
if($num == $year)
{
echo "<option value=$num selected>$num</option>";
}
else
{
echo "<option value=$num>$num</option>";
}
}
echo "</select> / ";
$i = 1;
// The Month drop-down menu
echo "<select name=month>";
foreach($month_array as $num)
{
if($num_array[$i] == $month)
{
echo "<option value=$num_array[$i] selected>$num</option>";
}
else
{
echo "<option value=$num_array[$i]>$num</option>";
}
$i++;
}
echo "</select>";
// The Day drop-down menu
echo "<select name=day>";
for($num = 1; $num < 32; $num++)
{
if($num == $day)
{
echo "<option value=$num_array[$num] selected>$num_array[$num]</option> ";
}
else
{
echo "<option value=$num_array[$num]>$num_array[$num]</option>";
}
}
echo "</select> ";
// The Hour drop-down menu
echo "<select name=hour>";
for($num = 0; $num < 24; $num++)
{
if($num_array[$num] == $hour)
{
echo "<option value=$num_array[$num] selected> $num_array[$num] </option>";
}
else
{
echo "<option value=$num_array[$num]> $num_array[$num] </option>";
}
}
echo "</select> : ";
// The Minute drop-down menu
echo "<select name=minute>";
for($num = 0; $num < 12; $num++)
{
if($min_array[$num] == $min)
{
echo "<option value=$min_array[$num] selected>$min_array[$num]</option>";
}
else
{
echo "<option value=$min_array[$num]>$min_array[$num]</option>";
}
}
echo "</select>";
}
}
For the most part this was a quick drop-down menu for a company project, so technicians could set the date on tickets. In other words, it was made to be bare-bones to provide a utility that would need manual input.
Using our Script
Since this script was made with simplicity in mind, we only need to include our script, create a new instance of the class, and run our date_menu function:
<?php
// test.php showing the initialization of the class and
// function for those not completely familiar with doing so
require("includes/datetime.php");
$init = new menus();
$resp = $init->date_menu();
?>
What’s Missing
You may already notice that this is a very minimum implementation, so it doesn’t automatically set the number of days based on the month. In the same regard, it doesn’t figure for leap-years. More or less this is a decent starting point for a fully fledged drop-down.
Maybe later I will update this with the version that automatically reduces the day list depending on the month, and if it is a leap year. Stay tuned!
No Comments