You are here

function _scheduler_strptime in Scheduler 6

Same name and namespace in other branches
  1. 5 scheduler.module \_scheduler_strptime()
  2. 7 scheduler.module \_scheduler_strptime()

Parse a time/date as UTC time

Parameters

string $date The string to parse:

string $format The format used in $date. See date() (http://www.php.net/manual/en/function.date.php): specification for format options. Right now only dHhmiaAsyY are supported.

Return value

the parsed time as a UTC timestamp

See also

date()

1 call to _scheduler_strptime()
_scheduler_strtotime in ./scheduler.module
Converts an english time string ('Y-m-d H:i:s') from the users timezone into an unix timestamp

File

./scheduler.module, line 514

Code

function _scheduler_strptime($date, $format) {

  # we need to build a regex pattern for the date format
  $date_entities = array(
    'd',
    'H',
    'h',
    'm',
    'i',
    'a',
    'A',
    's',
    'y',
    'Y',
    'n',
    'j',
    'g',
    'G',
  );
  $date_regex_replacements = array(
    '(\\d{2})',
    '(\\d{2})',
    '(\\d{2})',
    '(\\d{2})',
    '(\\d{2})',
    '([ap]m)',
    '([AP]M)',
    '(\\d{2})',
    '(\\d{2})',
    '(\\d{4})',
    '(\\d{1,2})',
    '(\\d{1,2})',
    '(\\d{1,2})',
    '(\\d{1,2})',
  );
  $custom_pattern = str_replace($date_entities, $date_regex_replacements, $format);
  if (!preg_match("#{$custom_pattern}#", $date, $value_matches)) {
    return FALSE;
  }
  if (!preg_match_all("/(\\w)/", $format, $entity_matches)) {
    return FALSE;
  }
  $results = array(
    'day' => 0,
    'hour' => 0,
    'month' => 0,
    'minute' => 0,
    'meridiem' => NULL,
    'second' => 0,
    'year' => 0,
  );
  $index = 1;
  foreach ($entity_matches[1] as $entity) {
    $value = intval($value_matches[$index]);
    switch ($entity) {
      case 'd':
      case 'j':
        $results['day'] = $value;
        break;
      case 'H':
      case 'h':
      case 'g':
      case 'G':
        $results['hour'] = $value;
        break;
      case 'm':
      case 'n':
        $results['month'] = $value;
        break;
      case 'i':
        $results['minute'] = $value;
        break;
      case 'a':
      case 'A':
        $results['meridiem'] = $value_matches[$index];
        break;
      case 's':
        $results['second'] = $value;
        break;
      case 'y':
      case 'Y':
        $results['year'] = $value;
        break;
    }
    $index++;
  }
  if (strncasecmp($results['meridiem'], "pm", 2) == 0 && $results['hour'] < 12) {
    $results['hour'] += 12;
  }
  if (strncasecmp($results['meridiem'], "am", 2) == 0 && $results['hour'] == 12) {
    $results['hour'] = 0;
  }
  $time = gmmktime($results['hour'], $results['minute'], $results['second'], $results['month'], $results['day'], $results['year']);
  return $time;
}