You are here

function webform_time_convert in Webform 7.3

Same name and namespace in other branches
  1. 6.3 components/time.inc \webform_time_convert()
  2. 7.4 components/time.inc \webform_time_convert()

Convert a time between a 24-hour and a 12-hour value.

Parameters

$array: An array of hour, minute, second, and optionally ampm.

$format: Either 12-hour or 24-hour.

Return value

An array with hour, minute, second, and ampm (if using "12-hour").

5 calls to webform_time_convert()
webform_expand_time in components/time.inc
Form API #process function for Webform time fields.
_webform_csv_data_time in components/time.inc
Implements _webform_csv_data_component().
_webform_display_time in components/time.inc
Implements _webform_display_component().
_webform_submit_time in components/time.inc
Implements _webform_submit_component().
_webform_table_time in components/time.inc
Implements _webform_table_component().

File

components/time.inc, line 421
Webform module time component.

Code

function webform_time_convert($array, $format) {
  if ($array['hour'] !== '') {
    if ($format == '12-hour') {
      $array['ampm'] = $array['hour'] >= 12 && $array['hour'] < 24 ? 'pm' : 'am';
      $array['hour'] = $array['hour'] > 12 || $array['hour'] == 0 ? abs($array['hour'] - 12) : (int) $array['hour'];
    }
    elseif ($format == '24-hour' && isset($array['ampm'])) {
      $array['hour'] = $array['hour'] < 12 && $array['ampm'] == 'pm' ? $array['hour'] + 12 : (int) $array['hour'];
      $array['hour'] = $array['hour'] == 12 && $array['ampm'] == 'am' ? 0 : $array['hour'];
    }
  }
  if ($format == '12-hour' && !isset($array['ampm'])) {
    $array['ampm'] = '';
  }
  elseif ($format == '24-hour' && isset($array['ampm'])) {
    unset($array['ampm']);
  }
  return $array;
}