You are here

function date_convert_timezone in Date 5

Timezone conversion function

Parameters

$date - the date object: @param $timezone_in - 'none', 'GMT', or timezone name in format 'US/Central' @param $timezone_out - 'none', 'GMT', or timezone name in format 'US/Central' @param $type - 'db' or 'local', the part of the date object to be created

7 calls to date_convert_timezone()
date_data_integrity in ./date.install
Progressive update of date information, integrity checking of all date values.
date_field_object in ./date.module
Use the Date API to get an object representation of a date field
date_ical_date in ./date_api_ical.inc
Adjust ical date to appropriate timezone and format it.
date_select_input in ./date.inc
Flexible Date/Time Drop-Down Selector
date_set_date in ./date.inc
Function to set local and db date parts in the date object

... See full list

File

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

Code

function date_convert_timezone(&$date, $timezone_in = 'GMT', $timezone_out = 'GMT', $type = 'db') {
  $fromtype = $type == 'db' ? 'local' : 'db';

  // skip timezone conversion if no timezone conversion was desired
  // set the local and db parts of the date object to be the same and return
  if ($timezone_in == 'none' || $timezone_out == 'none' || !isset($date->{$fromtype}->parts[0])) {
    if (($date->local->iso || $date->local->iso == 0) && !$date->db->iso) {
      date_set_date($date, $date->local->iso, $timezone_out, 'db', DATE_ISO);
    }
    elseif (($date->db->iso || $date->db->iso == 0) && !$date->local->iso) {
      date_set_date($date, $date->db->iso, $timezone_out, 'local', DATE_ISO);
    }
    return;
  }
  if ($type == 'local') {
    $date->local->timezone = $timezone_out;

    // attempt conversion only when there is available data to use
    if (!$date->db->iso && !$date->db->iso == 0) {
      return;
    }

    // see if an offset applies, and adjust date accordingly
    if ($offset = date_offset(date_gmgetdate($date->db->timestamp), $date->local->timezone)) {
      $out_date = $date->db->timestamp + $offset;
      date_set_date($date, $out_date, $timezone_out, $type, DATE_UNIX);
      $date->local->offset = $offset;

      // no offset, just set other part of date object to same value
    }
    else {
      date_set_date($date, $date->db->iso, $timezone_out, $type, DATE_ISO);
      $date->local->offset = 0;
    }
  }
  else {

    // attempt conversion only when there is available data to use
    if (!$date->local->iso && !$date->local->iso == 0 || !$date->local->timezone) {
      return;
    }

    // see if an offset applies, and adjust date accordingly
    if ($offset = date_offset(date_gmgetdate($date->local->timestamp), $date->local->timezone)) {
      $out_date = $date->local->timestamp - $offset;
      date_set_date($date, $out_date, $timezone_out, $type, DATE_UNIX);
      $date->local->offset = $offset;
    }
    else {
      date_set_date($date, $date->local->iso, $timezone_out, $type, DATE_ISO);
      $date->local->offset = 0;
    }
  }
  return;
}