You are here

function webform_time_convert in Webform 7.4

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

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

Parameters

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

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

Return value

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

6 calls to webform_time_convert()
webform_conditional_value_datetime in includes/webform.conditionals.inc
Utility function to convert incoming time and dates into strings.
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().

... See full list

File

components/time.inc, line 506
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;
}