MySQL date format is like YYYY-MM-DD HH:mm:ss, it is a very time-consuming and annoying task to create piece of code that edit datetime column, so I’ve crafted two pair of functions that dump the datetime value into sets of HTML forms for editing date and times.

This function dumps the datetime value into a simple HTML forms consists of a textbox for editing day, a single selectbox for month entry and year textbox. If there’s time found in datetime value, it will popup the hour, minutes and seconds textbox.


/**
 * Dump the $datetime value into HTML forms
 * $datetime => the value to be dumped, the format should be:
 *   yyyy-MM-dd HH:ii:ss, as MySQL date format
 * $name => the HTML form name prefix
 */
function date_view_form($datetime, $name)
{
	$o = array();
	
	@list($date, $time) = explode(" ", $datetime);
	if (FALSE !== strpos($date, ":")) {
		// date is time
		$time = $date;
		$date = "";
	}
	
	// dump the date part
	if ($date) {
		list($year, $month, $day) = explode("-", $date);
		$dateform[] = '<input id="'.$name.'_year" name="'.
			$name.'_year" type="text" size="4" value="'.
			str_pad($year, "0", 4, STR_PAD_LEFT).'" />';
		$dateform[] = select_month($name . "_month", $month);
		$dateform[] = '<input id="'.$name.'_day" name="'.
			$name.'_day" type="text" size="2" value="'.
			str_pad($day, "0", 2, STR_PAD_LEFT).'" />';
		$o[] = implode("-", $dateform) . "n";
	}
	
	// if has time part, create the time part aswell
	if ($time) {
		list($hour, $min, $sec) = explode(":", $time);
		$timeform[] = '<input id="'.$name.'_hour" name="'.$name.
			'_hour" type="text" size="2" value="'.
			str_pad($hour, "0", 2, STR_PAD_LEFT).'" />';
		$timeform[] = '<input id="'.$name.'_min" name="'.
			$name.'_min" type="text" size="2" value="'.
			str_pad($min, "0", 2, STR_PAD_LEFT).'" />';
		$timeform[] = '<input id="'.$name.'_sec" name="'.
			$name.'_sec" type="text" size="2" value="'.
			str_pad($sec, "0", 2, STR_PAD_LEFT).'" />';
		$o[] = implode(":", $timeform) . "n";
	}
	
	return implode("n", $o);
}

/**
 * Get the dumped HTML forms, and returns the result as a valid
 * MySQL date format, if not valid, return a default value, predefined as 
 * FALSE
 * $name => the HTML form name prefix we put
 * $var => where we going to get the value, try using $_GET or $_POST
 * $default => default value
 */
function date_get_value($name, $var, $default=FALSE)
{
	if (FALSE === $default)
		$default = date("Y-m-d");
	
	$fields = explode(",", "year,month,day,hour,min,sec");
	foreach($fields as $field) {
		$key = "{$name}_{$field}";
		$$field = isset($var[$key]) ? intval($var[$key]) : 0;
	}
	
	$stamp = mktime($hour, $min, $sec, $month, $day, $year);
	if ($stamp <= 0) return $default;
	
	if ($hour == 0 &amp;&amp; $min == 0 &amp;&amp; $sec == 0) return date("Y-m-d", $stamp);
	else return date("Y-m-d H:i:s", $stamp);
}

/**
 * Dump a SelectBox of monthnames, please replace the month_names value, since
 * its in Indonesian.
 *
 * $name => The HTML form name
 * $value => the Selected Value
 */
function select_month($name, $value="")
{
	if ("" == $value) $value = date("m");
	
	$month_names = explode(",", ",Januari,Februari,Maret,April,Mei,Juni,".
		"Juli,Agustus,September,Oktober,November,Desember");
	unset($month_names[0]);
	
	$o = array();
	foreach($month_names as $key=>$text) {
		$s = $value == $key ? ' selected="selected"' : '';
		$o[] = '<option value="'.$key.'"'.$s.'>'.$text.'</option>';
	}
	return '<select name="'.$name.'">'.implode("n", $o).'</select>';
}

There are many JavaScript library that perform a date-form with a nice calendar input, but why the Hell am I using only textbox and SelectBox? The answer is simplicity. I want a dead-simple-fast date-form that operator like and can be operated using only arrow keys and tabs to manage, and JavaScript calendar widget are overkill.

Please try the demo page,
just to feel the form, or you can download the source code.