Skip to content

Instantly share code, notes, and snippets.

@MrMaksimize
Created May 7, 2012 15:01
Show Gist options
  • Save MrMaksimize/2628239 to your computer and use it in GitHub Desktop.
Save MrMaksimize/2628239 to your computer and use it in GitHub Desktop.
<?php
/**
* Converts a date string to a date object with requested time zone
*
* @param $date
* Date String
* @param $from_tz
* String of timezone to convert FROM
* @param $to_tz
* String of timezone to convert TO
* @param $return_type
* How should the result be returned.
* if NULL, date object is returned
* if it's an array of formats, the parts are returned in an array
*/
function _date_convert_tz($date, $from_tz = 'UTC', $to_tz = 'UTC', $return_type = NULL) {
// make a date object, initialize with $from_tz
$date = date_make_date($date, $from_tz, $type= DATE_ISO);
// check if conversion is needed
if ($from_tz != $to_tz) {
date_timezone_set($date, timezone_open($to_tz));
}
// if the return type is null return date object
if (is_null($return_type)) {
return $date;
}
if (is_string($return_type)) {
return date_format_date($date, 'custom', $return_type);
}
// otherwise return formatted array
$return = array();
foreach ($return_type as $key => $format) {
$return[$key] = date_format_date($date, 'custom', $format);
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment