Brendan Murty

Home > Posts > Date picker

A simple date picker designed for use on mobile devices. Creates three dropdown menus allowing for selection of dates in the last 10 years. Also allows for customising the currently selected date, which defaults to today's date.

Date picker The finished result

// form_date_picker
// Purpose: Create a date picker allowing selection of dates in the next 10 years
// Notes: Defaults to selecting today but custom selected date can be set (in YYYY-MM-DD format)
// Example: <?= form_date_picker('2012-12-25'); ?>
function form_date_picker($custom_date=''){

	if($custom_date==''){ // Use todays date
	}else{ // Use a custom date

	// Day
	$f='<select id="date_day" name="date_day">';
		if($n<10){ $n='0'.$n; }
		$f.='<option label="'.$i.'" value="'.$n.'"';
		if($i==$selected_day){ $f.=' selected="selected"'; }

	// Month
	$f.='<select id="date_month" name="date_month">';
		if($i<10){ $i='0'.$i; }
		$f.='<option label="'.$l.'" value="'.$i.'"';
		if($i==$selected_month){ $f.=' selected="selected"'; }

	// Year
	$f.='<select id="date_year" name="date_year">';
		$f.='<option label="'.$i.'" value="'.$i.'"';
		if($i==$selected_year){ $f.=' selected="selected"'; }

	return $f;