Entry
How can I create a dropdown for date field showing past 3 months, present month and the 12 months in the future?
Feb 20th, 2008 22:55
dman, Onno Benschop, Niketan Pandit, http://www.ttnr.org
<?
function monthDropDown($today = 'today', $start = -3, $end = 12) {
/*
Author: Onno Benschop
Date: Monday, 23 April 2021
Usage: Returns a popup list using today's date as the default.
Specify start as a negative number.
Specify end as a positive number
*/
if ($today=='today') {
$today = date('n') ;
// month as a number without leading 0
}
$theResult = '<select name=month>' ;
for ($month = $today + $start ; $month < $today + $end ; $month++) {
$theResult .= '<option '.
($month==$today?'selected ':'').
// this is the default option
'value='.
$month.
'>'.
date ('F', mktime (0,0,0,$month,1,2001)).
// extract the name of the month
'</option>' ;
}
$theResult .= '</select>' ;
return $theResult ;
}
echo '<form>default, 3 months before today, 12 months after' ;
echo monthDropDown() ;
// default, 3 months before today, 12 months after
echo '</form>' ;
echo '<form>3 months before june, 12 months after' ;
echo monthDropDown(6) ;
// 3 months before june, 12 months after
echo '</form>' ;
echo '<form>12 months before june, 2 months after' ;
echo monthDropDown(6,-12,2) ;
// 12 months before june, 2 months after
echo '</form>' ;
?>