You are here

function _calendar_systems_date_convert in Calendar Systems 7

Same name and namespace in other branches
  1. 8 calendar_systems.helpers.inc \_calendar_systems_date_convert()
  2. 7.2 calendar_systems.helpers.inc \_calendar_systems_date_convert()

Parameters

$date:

null $format:

Return value

array|bool|int|mixed|null|string

Internal helper method to convert and validate date

2 calls to _calendar_systems_date_convert()
calendar_systems_date_validate in ./calendar_systems.module
Element validation callback for date fields.
_calendar_systems_views_exposed_form_validate in ./calendar_systems.module
@todo supporting month and day only filters and contextual filters

File

./calendar_systems.helpers.inc, line 279

Code

function _calendar_systems_date_convert($date, $format = NULL, $calendar_system = NULL, $options = array()) {
  if (is_null($calendar_system)) {
    $calendar_system = calendar_systems_get_calendar_system_name();
  }
  if ($calendar_system == 'default') {
    return $date;
  }

  // Get proper calendar instance:
  $calendar = calendar_systems_get_calendar_instance($calendar_system);
  $result = FALSE;
  if (is_array($date)) {
    $r = $calendar
      ->isDateValid(isset($date['month']) ? $date['month'] : NULL, isset($date['day']) ? $date['day'] : NULL, isset($date['year']) ? $date['year'] : NULL);
    if ($r !== 0 && $r !== FALSE) {
      list($date['year'], $date['month'], $date['day']) = $calendar
        ->toGregorian($date['year'], $date['month'], $date['day']);
      $result = $date;
    }
  }
  else {
    $date_parts = array();
    if (isset($options['iso_fixed_size_position']) && $options['iso_fixed_size_position']) {
      $format_parser = '(?<year>[0-9]{4})(?<month>[0-9]{2})?(?<day>[0-9]{2})?(?<hour>[0-9]{2})?(?<minute>[0-9]{2})?(?<second>[0-9]{2})?';
      if (preg_match_all("%{$format_parser}%", $date, $matches)) {
        $date_parts['year'] = @$matches['year'][0];
        $date_parts['month'] = @$matches['month'][0];
        $date_parts['day'] = @$matches['day'][0];
        $date_parts['hour'] = @$matches['hour'][0];
        $date_parts['minute'] = @$matches['minute'][0];
        $date_parts['second'] = @$matches['second'][0];
      }
    }
    elseif (!is_null($format)) {
      $format = preg_quote($format);
      $format_parser = preg_replace('%([a-zA-Z])%', '(?<$1>[0-9]+)', $format);

      //dDjFmMnYy
      if (preg_match_all("%{$format_parser}%", $date, $matches)) {
        $date_parts['year'] = @$matches['Y'][0];
        $date_parts['month'] = @$matches['m'][0];
        $date_parts['day'] = @$matches['d'][0];
        $date_parts['hour'] = @$matches['H'][0];
        $date_parts['minute'] = @$matches['i'][0];
        $date_parts['second'] = @$matches['s'][0];
      }
    }
    else {

      // Extract date and time:
      $_date = preg_replace('/([0-9]+\\-[0-9]+\\-[0-9]+) (.*)/si', '$1', $date);
      $time = preg_replace('/([0-9]+\\-[0-9]+\\-[0-9]+) (.*)/si', '$2', $date);
      if (preg_match('/^([0-9]{2,4})[-\\/\\\\]([0-9]{1,2})[-\\/\\\\]([0-9]{1,2})( +([0-9]{1,2})[:]([0-9]{1,2})[:]([0-9]{1,2}))?/', $_date, $regs)) {
        $date_parts['year'] = $regs[1];
        $date_parts['month'] = $regs[2];
        $date_parts['day'] = $regs[3];
        $format = "Y-m-d {$time}";
      }
    }

    // Convert to timestamp:
    if ($date_parts) {
      if (@empty($date_parts['month'])) {
        $date_parts['month'] = 1;
      }
      if (@empty($date_parts['day'])) {
        $date_parts['day'] = 1;
      }
      $r = $calendar
        ->isDateValid($date_parts['month'], $date_parts['day'], $date_parts['year']);
      if ($r !== 0 && $r !== FALSE) {
        $result = $calendar
          ->strToTimestamp("{$date_parts['year']}-{$date_parts['month']}-{$date_parts['day']} 10:10:10");
        $result = date($format, $result);
      }
    }
  }
  return $result;
}