Software Developer

Convert Hour from 12-Hour Format to 24-Hour Military Format

Converts just the hour part of a time, from 12-hour format into 24-hour format ( military time ).
See php comments for specifics of this function. You may need to modify what you don’t need, such as the meridiem parameter.

Note: This function only processes the hour part of the time, not the minutes.

/**
 * convert just the hour part of a time from 12-hour format into 24-hour format
 * @param string or int, the hour in 12-hour format, 1 - 12
 * @param $meridiem, accepts only string "a.m." or "p.m."
 * returns string "00", or "1" through "12", or integer 13 through 23
 */

function hour12to24($h12, $meridiem) {
 		if(($meridiem == 'a.m.') && ($h12 == '12')) {
			$mh = '00';
		} elseif( (($meridiem == 'a.m.') && ($h12 != '12')) || (($meridiem == 'p.m.') && ($h12 == '12')) ) {
			$mh = $h12;
		} elseif( ($meridiem == 'p.m.') && in_array($h12, array('1','2','3','4','5','6','7','8','9','10','11')) )  {
			$avc = (int) $h12;// conver to integer, then add 12.
			$mh = $avc + 12;
		}
		return $mh;// military hour
}

See more: , ,

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]