You are here

function date_text2iso in Date 5

Use stringtotime function to create an iso date out of text

3 calls to date_text2iso()
date_text2unix in ./date.inc
date_text_make_dbdate in ./date.inc
Construct a value to save to the database from text input
date_text_validate in ./date.inc
Validation for text input

File

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

Code

function date_text2iso($text, $format) {

  // if date supplied in iso format, use it
  // TODO : there's probably better to do...
  // (do we _want_ to enter ISO formats ? if so we should have it in the input format settings)
  if (date_preg($text) && date_part_is_valid(date_iso_year($text), 'year')) {
    return $text;
  }

  // if not iso date, try to parse in the given format
  $custom = date_custom2iso($text, $format);
  if ($custom !== 'ERROR') {
    return $custom;
  }

  // if custom parsing failed, try strtotime conversion
  $iso = date_gmdate(DATE_STRING_ISO, strtotime($text . ' UTC'));
  if (date_part_is_valid(date_iso_year($iso), 'year')) {
    return $iso;
  }
  else {
    return 'ERROR';
  }
}