Software Developer

Detect if WP Time Format is in 24-hr Format

This function lets you detect if the Time Format in WordPress settings is set to a 24-hour format. It returns true if the Time Format is in a 24-hour format, otherwise it returns false. (The Time Format is also known as the ‘time_format’ option, and you can get it like this: get_option( 'time_format' ).)

/**
 * Detect if time_format is in 24hr format
 * @return bool true if hour is in 24hr format, otherwise false.
 */
function isa_is_time_format_24_hour() {

	$out = false;

	// some possible separators in the time format string, besides a 'space'
	$sep 					= array( ':', ',', '-', '/', '.' );
	
	$time_format			= get_option( 'time_format' );
	$time_format_elements 	= array_filter( explode( ' ', str_replace( $sep, ' ', $time_format ) ) );
	$time_format_elements 	= array_values( $time_format_elements );
	$hour_24hr_formats		= array( 'G', 'H' );

	// check if any time format elements are a 24hr format
	foreach( $hour_24hr_formats as $element ) {
		if ( in_array( $element, $time_format_elements ) ) {
			$out = true;
		}
	}

	return $out;
}

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]