Forum Moderators: coopster

Message Too Old, No Replies

Preloading 'date' combobox with current date

Preloading 'date' combobox with current date

         

eball

3:19 am on Feb 26, 2009 (gmt 0)

10+ Year Member



I was looking around the other day for a way to populate a 'date' dropdown/combobox layout with the current date via PHP. I was unable to find a suitable solution in a timely manner, and eventually ended up writing my own simple fix.

This particular layout consists of 2 separate select boxes (one for the month and one for the day of the month) and a simple year echo. I did not need a selectable year field for this project, so it's merely an echo of the current year.

There may be an easier, more efficient way to do this, but I was unable to find one. This is what worked for me. I thought it might be useful to others here in the forums, so without further ado;


$date = date("j"); // Single to double digit representation of the day without trailing zeros; '1 - 31'
$month = date("F"); // Full textual representation of the month; 'January - February'
// Preloads current month into combobox/dropdown
<SELECT NAME="month" ID="month_ID">
<option value="January"<?php if ($month == 'January') echo "SELECTED"; ?>>January
<option value="February"<?php if ($month == 'February') echo "SELECTED"; ?>>February
<option value="March"<?php if ($month == 'March') echo "SELECTED"; ?>>March
<option value="April"<?php if ($month == 'April') echo "SELECTED"; ?>>April
<option value="May"<?php if ($month == 'May') echo "SELECTED"; ?>>May
<option value="June"<?php if ($month == 'June') echo "SELECTED"; ?>>June
<option value="July"<?php if ($month == 'July') echo "SELECTED"; ?>>July
<option value="August"<?php if ($month == 'August') echo "SELECTED"; ?>>August
<option value="September"<?php if ($month == 'September') echo "SELECTED"; ?>>September
<option value="October"<?php if ($month == 'October') echo "SELECTED"; ?>>October
<option value="November"<?php if ($month == 'November') echo "SELECTED"; ?>>November
<option value="December"<?php if ($month == 'December') echo "SELECTED"; ?>>December
</select>
// Preloads current date (day of the month) into box
<select name="day" id="day_ID">
<option value="1"<?php if ($date == '1') echo "SELECTED"; ?>>1
<option value="2"<?php if ($date == '2') echo "SELECTED"; ?>>2
<option value="3"<?php if ($date == '3') echo "SELECTED"; ?>>3
<option value="4"<?php if ($date == '4') echo "SELECTED"; ?>>4
<option value="5"<?php if ($date == '5') echo "SELECTED"; ?>>5
<option value="6"<?php if ($date == '6') echo "SELECTED"; ?>>6
<option value="7"<?php if ($date == '7') echo "SELECTED"; ?>>7
<option value="8"<?php if ($date == '8') echo "SELECTED"; ?>>8
<option value="9"<?php if ($date == '9') echo "SELECTED"; ?>>9
<option value="10"<?php if ($date == '10') echo "SELECTED"; ?>>10
<option value="11"<?php if ($date == '11') echo "SELECTED"; ?>>11
<option value="12"<?php if ($date == '12') echo "SELECTED"; ?>>12
<option value="13"<?php if ($date == '13') echo "SELECTED"; ?>>13
<option value="14"<?php if ($date == '14') echo "SELECTED"; ?>>14
<option value="15"<?php if ($date == '15') echo "SELECTED"; ?>>15
<option value="16"<?php if ($date == '16') echo "SELECTED"; ?>>16
<option value="17"<?php if ($date == '17') echo "SELECTED"; ?>>17
<option value="18"<?php if ($date == '18') echo "SELECTED"; ?>>18
<option value="19"<?php if ($date == '19') echo "SELECTED"; ?>>19
<option value="20"<?php if ($date == '20') echo "SELECTED"; ?>>20
<option value="21"<?php if ($date == '21') echo "SELECTED"; ?>>21
<option value="22"<?php if ($date == '22') echo "SELECTED"; ?>>22
<option value="23"<?php if ($date == '23') echo "SELECTED"; ?>>23
<option value="24"<?php if ($date == '24') echo "SELECTED"; ?>>24
<option value="25"<?php if ($date == '25') echo "SELECTED"; ?>>25
<option value="26"<?php if ($date == '26') echo "SELECTED"; ?>>26
<option value="27"<?php if ($date == '27') echo "SELECTED"; ?>>27
<option value="28"<?php if ($date == '28') echo "SELECTED"; ?>>28
<option value="29"<?php if ($date == '29') echo "SELECTED"; ?>>29
<option value="30"<?php if ($date == '30') echo "SELECTED"; ?>>30
<option value="31"<?php if ($date == '31') echo "SELECTED"; ?>>31
</select>
<input type=text name="year" id="year_ID" size=4 maxlength=4 value="<?php echo date("Y");?>"/>

Of course, this may be modified to whatever date format you may use. This is just the format I needed for my particular project.

I hope someone finds this useful, I know I did :)

blang

4:29 am on Feb 26, 2009 (gmt 0)

10+ Year Member



Thanks for posting that. Immediately I thought "hey this needs some loops for the boring stuff". Here's my rendition, hope it's useful to you and gives you some ideas.

<?php
// some date variables to start
$curr_day_of_month= date('j');
$curr_month_numeric= date('n');
$curr_year= date('Y');
// months (no way around this that I can think of)
$months= array(
1=>'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);
// range of years (can be easily altered)
$years= range(1970,2030);
/*********************************************************************/
// begin the month SELECT element set
echo '<SELECT name=\'months\' id=\'months\'>';
// iterate through the 'months' array
foreach( $months AS $k => $v ) {
// begin month option set
echo "<OPTION value='{$k}'";
// ternary operator to select current month
echo ( $k == $curr_month_numeric ) ? ' SELECTED ' : '';
echo ">{$v}</OPTION>\n";
}
echo '</SELECT>';
// begin the days SELECT element set
echo '<SELECT name=\'days\' id=\'days\'>';
// iterate through 31 days
for ( $i=1; $i <= 31; $i++ ) {
// begin day option set
echo "<OPTION value='{$i}'";
// ternary operator to select current day
echo ( $i == $curr_day_of_month ) ? ' SELECTED ' : '';
echo ">{$i}</OPTION>\n";
}
echo '</SELECT>';
// begin the years SELECT element set
echo '<SELECT name=\'years\ id=\'years\'>';
// iterate through each year
foreach ( $years AS $v ) {
// begin year option set
echo "<OPTION value='{$v}'";
// ternary operator to select current year
echo ( $v == $curr_year ) ? ' SELECTED ' : '';
echo ">{$v}</OPTION>\n";
}
echo '</SELECT>';
?>

As an afterthought, it would probably be a good idea to find a way to 'gray out' the days that don't exist in the current month (e.g. this month only has 28 days). A simple class selector and conditional in the loop would do it.