PHP 24 hour time select element

Complete PHP function to display a select box with time of day options. They're neatly organized into option groups by morning, afternoon, evening and night.

The function will also handle selecting the appropriate default value.
function outputSelectTimes($fieldName, $defaultValue) {
	
	$default = date('G:i', strtotime($defaultValue));
	
	echo '<select name="' . $fieldName . '">';

	/* morning */
	echo '<optgroup label="Morning">';
	for ($i = 5; $i <= 11; $i++):
		echo '<option value="' . $i . ':' . '00"' . ($default == ($i . ':' . '00') ? ' selected' : '') . '>' . $i . ':00 AM</option>';
		echo '<option value="' . $i . ':' . '15"' . ($default == ($i . ':' . '15') ? ' selected' : '') . '>' . $i . ':15 AM</option>';
		echo '<option value="' . $i . ':' . '30"' . ($default == ($i . ':' . '30') ? ' selected' : '') . '>' . $i . ':30 AM</option>';
		echo '<option value="' . $i . ':' . '45"' . ($default == ($i . ':' . '45') ? ' selected' : '') . '>' . $i . ':45 AM</option>';
	endfor;
	echo '</optgroup>';

	/* afternoon */
	echo '<optgroup label="Afternoon">';
	echo '<option value="12:00"' . ($default == ('12:00') ? ' selected' : '') . '>12:00 PM</option>';
	echo '<option value="12:15"' . ($default == ('12:15') ? ' selected' : '') . '>12:15 PM</option>';
	echo '<option value="12:30"' . ($default == ('12:30') ? ' selected' : '') . '>12:30 PM</option>';
	echo '<option value="12:45"' . ($default == ('12:45') ? ' selected' : '') . '>12:45 PM</option>';
	for ($i = 1; $i <= 5; $i++):
		echo '<option value="' . ($i + 12) . ':' . '00"' . ($default == (($i + 12) . ':' . '00') ? ' selected' : '') . '>' . $i . ':00 PM</option>';
		echo '<option value="' . ($i + 12) . ':' . '15"' . ($default == (($i + 12) . ':' . '15') ? ' selected' : '') . '>' . $i . ':15 PM</option>';
		echo '<option value="' . ($i + 12) . ':' . '30"' . ($default == (($i + 12) . ':' . '30') ? ' selected' : '') . '>' . $i . ':30 PM</option>';
		echo '<option value="' . ($i + 12) . ':' . '45"' . ($default == (($i + 12) . ':' . '45') ? ' selected' : '') . '>' . $i . ':45 PM</option>';
	endfor;
	echo '</optgroup>';

	/* evening */
	echo '<optgroup label="Evening">';
	for ($i = 6; $i <= 11; $i++):
		echo '<option value="' . ($i + 12) . ':' . '00"' . ($default == (($i + 12) . ':' . '00') ? ' selected' : '') . '>' . $i . ':00 PM</option>';
		echo '<option value="' . ($i + 12) . ':' . '15"' . ($default == (($i + 12) . ':' . '15') ? ' selected' : '') . '>' . $i . ':15 PM</option>';
		echo '<option value="' . ($i + 12) . ':' . '30"' . ($default == (($i + 12) . ':' . '30') ? ' selected' : '') . '>' . $i . ':30 PM</option>';
		echo '<option value="' . ($i + 12) . ':' . '45"' . ($default == (($i + 12) . ':' . '45') ? ' selected' : '') . '>' . $i . ':45 PM</option>';
	endfor;
	echo '</optgroup>';

	/* night */
	echo '<optgroup label="Night">';
	echo '<option value="0:00"' . ($default == ('0:00') ? ' selected' : '') . '>12:00 AM</option>';
	echo '<option value="0:15"' . ($default == ('0:15') ? ' selected' : '') . '>12:15 AM</option>';
	echo '<option value="0:30"' . ($default == ('0:30') ? ' selected' : '') . '>12:30 AM</option>';
	echo '<option value="0:45"' . ($default == ('0:45') ? ' selected' : '') . '>12:45 AM</option>';
	for ($i = 1; $i <= 4; $i++):
		echo '<option value="' . $i . ':' . '00"' . ($default == ($i . ':' . '00') ? ' selected' : '') . '>' . $i . ':00 AM</option>';
		echo '<option value="' . $i . ':' . '15"' . ($default == ($i . ':' . '15') ? ' selected' : '') . '>' . $i . ':15 AM</option>';
		echo '<option value="' . $i . ':' . '30"' . ($default == ($i . ':' . '30') ? ' selected' : '') . '>' . $i . ':30 AM</option>';
		echo '<option value="' . $i . ':' . '45"' . ($default == ($i . ':' . '45') ? ' selected' : '') . '>' . $i . ':45 AM</option>';
	endfor;
	echo '</optgroup>';
	
}
Posted by
Snippet Viewed 2610 times.

Share your PHP code snippets:

  • Get some recognition & link back to your site.

 

Submit | Browse

Most Recent php snippets

Most Viewed