You are here

function date_convert in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_api.module \date_convert()
  2. 6 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, except when handling timestamps because create_date() assumes timestamps hold the UTC value for the time.

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

string $tz: the timezone of the supplied value, only needed when using timestamps for dates not set to UTC.

14 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_convert_from_custom in ./date_api_elements.inc
Convert a date input in a custom format to a standard date type
date_days_in_year in ./date_api.module
Identify the number of days in a year for a date.
date_day_of_week in ./date_api.module
Returns day of week for a given date (0 = Sunday).

... See full list

File

./date_api.module, line 994
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, $tz = 'UTC') {
  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;
      }
      if (isset($date['ampm'])) {
        if ($date['ampm'] == 'pm' && $date['hour'] < 12) {
          $date['hour'] += 12;
        }
        if ($date['ampm'] == 'am' && $date['hour'] == 12) {
          $date['hour'] -= 12;
        }
      }
      $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($tz));
        case DATE_UNIX:
          $obj = date_create($datetime, timezone_open($tz));
          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 = date_fuzzy_datetime($date);
      $obj = date_create($date, timezone_open($tz));
      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($tz));
      break;
    case DATE_UNIX:
      if (!is_numeric($date)) {
        return NULL;
      }

      // Special case when creating dates with timestamps.
      // The date_create() function will assume date is UTC value
      // and will ignore our timezone.
      $obj = date_create("@{$date}", timezone_open('UTC'));
      date_timezone_set($obj, timezone_open($tz));
      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;
  }
}