You are here

function date_custom2iso in Date 5

5 calls to date_custom2iso()
date_custom2unix in ./date.inc
date_jscalendar_make_dbdate in ./date.inc
Construct a value to save to the database from jscalendar input
date_jscalendar_validate in ./date.inc
Validation for jscalendar input
date_text2iso in ./date.inc
Use stringtotime function to create an iso date out of text
_date_views_filter_handler in ./date_views.inc

File

./date.inc, line 1296
Date/time API functions

Code

function date_custom2iso($date, $format) {
  $array = array(
    "d" => "\\d{1,2}",
    // we allow 1 to be tolerant - maybe we shouldn't ?
    "D" => "\\S{3,4}",
    //Using S instead of w to pick up chars like German umlaute
    "j" => "\\d{1,2}",
    "l" => "\\S*",
    //Using S instead of w to pick up chars like German umlaute
    "N" => "\\d",
    "S" => "\\w{2}",
    "w" => "\\d",
    "z" => "\\d{1,3}",
    "W" => "\\d{1,2}",
    "F" => "\\S*",
    //Using S instead of w to pick up chars like German umlaute
    "m" => "\\d{2}",
    "M" => "\\S{3,4}",
    //Using S instead of w to pick up chars like German umlaute
    "n" => "\\d{1,2}",
    "t" => "\\d{2}",
    "L" => "\\d",
    "o" => "\\d{4}",
    "Y" => "\\d{4}",
    "y" => "\\d{2}",
    "a" => "am|pm",
    "A" => "AM|PM",
    "B" => "\\d{3}",
    "g" => "\\d{1,2}",
    "G" => "\\d{1,2}",
    "h" => "\\d{1,2}",
    // we allow 1 to be tolerant - maybe we shouldn't ?
    "H" => "\\d{1,2}",
    // we allow 1 to be tolerant - maybe we shouldn't ?
    "i" => "\\d{2}",
    "s" => "\\d{2}",
    "e" => "\\w*",
    "I" => "\\d",
    "O" => "[+-]?\\d{4}",
    "P" => "[+-]?\\d{2}\\:\\d{2}",
    "T" => "\\w*",
    "z" => "[+-]?\\d*",
    "c" => "*",
    "r" => "*",
    "U" => "\\d*",
  );
  foreach ($array as $key => $value) {
    $patterns[] = "`(^|[^\\\\\\\\])" . $key . "`";

    // the letter with no preceding '\'
    $repl1[] = '${1}(.)';

    // a single character
    $repl2[] = '${1}(' . $value . ')';

    // the value
  }
  $patterns[] = "`\\\\\\\\([" . implode(array_keys($array)) . "])`";
  $repl1[] = '${1}';
  $repl2[] = '${1}';
  $format_regexp = preg_quote($format);

  // extract letters
  $regex1 = preg_replace($patterns, $repl1, $format_regexp, 1);
  preg_match('`^' . $regex1 . '$`', stripslashes($format), $letters);
  array_shift($letters);

  // extract values
  $regex2 = preg_replace($patterns, $repl2, $format_regexp, 1);
  preg_match('`^' . $regex2 . '$`', $date, $values);
  array_shift($values);

  // if we did not find all the values for the patterns in the format, abort
  if (count($letters) != count($values)) {
    return 'ERROR';
  }
  $final_date['hours'] = "00";
  $final_date['minutes'] = "00";
  $final_date['seconds'] = "00";
  $final_date['mon'] = "00";
  $final_date['mday'] = "00";
  $final_date['year'] = "0000";
  foreach ($letters as $i => $letter) {
    $value = $values[$i];
    switch ($letter) {
      case 'd':
      case 'j':
        $final_date['mday'] = str_pad($value, 2, "0", STR_PAD_LEFT);
        break;
      case 'n':
      case 'm':
        $final_date['mon'] = str_pad($value, 2, "0", STR_PAD_LEFT);
        break;
      case 'F':
        $array_month_long = array(
          t('January') => 1,
          t('February') => 2,
          t('March') => 3,
          t('April') => 4,
          t('May') => 5,
          t('June') => 6,
          t('July') => 7,
          t('August') => 8,
          t('September') => 9,
          t('October') => 10,
          t('November') => 11,
          t('December') => 12,
        );
        $final_date['mon'] = str_pad($array_month_long[$value], 2, "0", STR_PAD_LEFT);
        break;
      case 'M':
        $array_month = array(
          t('Jan') => 1,
          t('Feb') => 2,
          t('Mar') => 3,
          t('Apr') => 4,
          t('May') => 5,
          t('Jun') => 6,
          t('Jul') => 7,
          t('Aug') => 8,
          t('Sep') => 9,
          t('Oct') => 10,
          t('Nov') => 11,
          t('Dec') => 12,
        );
        $final_date['mon'] = str_pad($array_month[$value], 2, "0", STR_PAD_LEFT);
        break;
      case 'Y':
      case 'y':
        $year = str_pad($value, 2, "0", STR_PAD_LEFT);

        // if no century, we add the current one ("06" => "2006")
        $final_date['year'] = str_pad($year, 4, substr(date("Y"), 0, 2), STR_PAD_LEFT);
        break;
      case 'a':
      case 'A':
        $am_pm = strtolower($value);
        break;
      case 'g':
      case 'h':
      case 'G':
      case 'H':
        $final_date['hours'] = str_pad($value, 2, "0", STR_PAD_LEFT);
        break;
      case 'i':
        $final_date['minutes'] = str_pad($value, 2, "0", STR_PAD_LEFT);
        break;
      case 's':
        $final_date['seconds'] = str_pad($value, 2, "0", STR_PAD_LEFT);
        break;
      case 'U':

        // TODO ?
        break;
    }
  }

  // TODO : add some validation ? day in [1..31], etc...
  switch ($am_pm) {
    case 'am':
      if ($final_date['hours'] == "12") {
        $final_date['hours'] = "00";
      }
      break;
    case 'pm':
      if ($final_date['hours'] != "12") {
        $final_date['hours'] += 12;
      }
      break;
  }
  return date_array2iso($final_date);
}