You are here

function date_convert in Date 6

Same name and namespace in other branches
  1. 5.2 date_api.module \date_convert()
  2. 6.2 date_api.module \date_convert()

Date conversion helper function.

A variety of ways to convert dates from one type to another. No timezone conversion is done in this operation!!

Example: date_convert('2007-03-15 08:30', DATE_DATETIME, DATE_UNIX); returns unix value for the supplied date.

Parameters

mixed $date: the date to convert

string $from_type: the type of date to convert from

string $to_type: the type of date to convert to

15 calls to date_convert()
DateAPI::testDateAPI in tests/date_api.test
date_api_ical_build_rrule in ./date_api_ical.inc
Build an iCal RULE from $form_values.
date_combo_validate in date/date_elements.inc
Validate and update a combo element. Don't try this if there were errors before reaching this point.
date_convert_from_custom in ./date_api_elements.inc
Convert a date input in a custom format to a standard date type
date_days_in_month in ./date_api.module
Identify the number of days in a month for a date.

... See full list

File

./date_api.module, line 898
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Code

function date_convert($date, $from_type, $to_type) {
  if (empty($date) && !$date === 0) {
    return NULL;
  }
  if (empty($from_type) || empty($to_type) || $from_type == $to_type) {
    return $date;
  }
  switch ($from_type) {
    case DATE_ARRAY:
      if (!is_array($date)) {
        return NULL;
      }
      $datetime = date_pad(intval($date['year']), 4) . '-' . date_pad(intval($date['month'])) . '-' . date_pad(intval($date['day'])) . ' ' . date_pad(intval($date['hour'])) . ':' . date_pad(intval($date['minute'])) . ':' . date_pad(intval($date['second']));
      switch ($to_type) {
        case DATE_ISO:
          return str_replace(' ', 'T', $datetime);
        case DATE_DATETIME:
          return $datetime;
        case DATE_ICAL:
          $replace = array(
            ' ' => 'T',
            '-' => '',
            ':' => '',
          );
          return strtr($datetime, $replace);
        case DATE_OBJECT:
          return date_create($datetime, timezone_open('UTC'));
        case DATE_UNIX:
          $obj = date_create($datetime, timezone_open('UTC'));
          return date_format($obj, 'U');
      }
      break;
    case DATE_OBJECT:
      if (!is_object($date)) {
        return NULL;
      }
      $obj = $date;
      break;
    case DATE_DATETIME:
    case DATE_ISO:
      if (!preg_match(DATE_REGEX_LOOSE, $date)) {
        return NULL;
      }
      $date = str_replace('T', ' ', $date);
      $obj = date_make_date(date_fuzzy_datetime($date), 'UTC');
      break;
    case DATE_ICAL:
      if (!preg_match(DATE_REGEX_LOOSE, $date)) {
        return NULL;
      }
      preg_match(DATE_REGEX_LOOSE, $date, $regs);
      $datetime = date_pad($regs[1], 4) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]) . 'T' . date_pad($regs[5]) . ':' . date_pad($regs[6]) . ':' . date_pad($regs[7]);
      $obj = date_create($datetime, timezone_open('UTC'));
    case DATE_UNIX:
      if (!is_numeric($date)) {
        return NULL;
      }
      $obj = date_create("@{$date}", timezone_open('UTC'));
      break;
  }
  switch ($to_type) {
    case DATE_OBJECT:
      return $obj;
    case DATE_DATETIME:
      return date_format($obj, DATE_FORMAT_DATETIME);
    case DATE_ISO:
      return date_format($obj, DATE_FORMAT_ISO);
    case DATE_ICAL:
      return date_format($obj, DATE_FORMAT_ICAL);
    case DATE_UNIX:
      return date_format($obj, 'U');
    case DATE_ARRAY:
      $date_array = date_array($obj);

      // ISO dates may contain zero values for some date parts,
      // make sure they don't get lost in the conversion.
      if ($from_type == DATE_ISO) {
        $date_array = array_merge($date_array, date_iso_array($date));
      }
      return $date_array;
    default:
      return NULL;
  }
}