本文整理汇总了PHP中DateTimeZone类的典型用法代码示例。如果您正苦于以下问题:PHP DateTimeZone类的具体用法?PHP DateTimeZone怎么用?PHP DateTimeZone使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DateTimeZone类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getOffset
/**
* Get the offset time between from timezone and to timezone.
* @param string $from_timezone Timezone departure
* @param string $to_timezone Timezone arrival
* @return seconds Timezone offset
*/
public static function getOffset($from_timezone = "", $to_timezone = "")
{
if ($from_timezone === "" || $to_timezone === "") {
return 0;
}
try {
// Create two timezone objects, one for Singapore (Taiwan) and one for
// Tokyo (Jakarta)
$dateTimeFromZone = new \DateTimeZone($from_timezone);
$dateTimeToZone = new \DateTimeZone($to_timezone);
$date = (new \DateTime())->format('Y-m-d H:i:s');
$datTimeFrom = new \DateTime($date, $dateTimeFromZone);
$dateTimeTo = new \DateTime($date, $dateTimeToZone);
// Calculate the GMT offset for the date/time contained in the $datTimeFrom
// object, but using the timezone rules as defined for Tokyo
// ($dateTimeToZone).
$timeOffset = $dateTimeToZone->getOffset($datTimeFrom);
$timeOffset2 = $dateTimeFromZone->getOffset($dateTimeTo);
// Should show int(32400) (for dates after Sat Sep 8 01:00:00 1951 JST).
$offset = $timeOffset - $timeOffset2;
return $offset;
} catch (\Exception $e) {
return 0;
}
}
示例2: testUsingYiiTimeZoneSwitcherWithPhpTimeFunction
public function testUsingYiiTimeZoneSwitcherWithPhpTimeFunction()
{
$oldTimeZone = Yii::app()->getTimeZone();
$dateTimeUtc = new DateTime();
$timeStamp = time();
//always UTC regardless of server timezone or any timezone setting.
Yii::app()->setTimeZone('UTC');
$dateStamp = date("Y-m-d G:i", $timeStamp);
Yii::app()->setTimeZone('America/Chicago');
$dateStamp2 = date("Y-m-d G:i", $timeStamp);
Yii::app()->setTimeZone('America/New_York');
$timeZoneObject = new DateTimeZone('America/New_York');
$offset = $timeZoneObject->getOffset(new DateTime());
$this->assertTrue($offset == -18000 || $offset == -14400);
$newYorkTimeZone = new DateTimeZone(date_default_timezone_get());
$offset1 = $newYorkTimeZone->getOffset($dateTimeUtc);
$offset2 = timezone_offset_get($newYorkTimeZone, $dateTimeUtc);
$this->assertEquals($offset, $offset1);
$this->assertEquals($offset, $offset2);
if ($offset == -18000) {
$offsetHours = 6;
} else {
$offsetHours = 5;
}
$dateStamp3 = date("Y-m-d G:i", $timeStamp);
$this->assertEquals(strtotime($dateStamp), strtotime($dateStamp2) + 3600 * $offsetHours);
// + 5 from GMT or +6 depending on DST
$this->assertEquals(strtotime($dateStamp3), strtotime($dateStamp2) + 3600 * 1);
// + 1 from NY
//Use retrieved offset based on timezone.
Yii::app()->setTimeZone($oldTimeZone);
}
示例3: get_nearest_timezone
public static function get_nearest_timezone($cur_lat, $cur_long, $country_code = '')
{
$timezone_ids = $country_code ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code) : DateTimeZone::listIdentifiers();
if ($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) {
$time_zone = '';
$tz_distance = 0;
//only one identifier?
if (count($timezone_ids) == 1) {
$time_zone = $timezone_ids[0];
} else {
foreach ($timezone_ids as $timezone_id) {
$timezone = new DateTimeZone($timezone_id);
$location = $timezone->getLocation();
$tz_lat = $location['latitude'];
$tz_long = $location['longitude'];
$theta = $cur_long - $tz_long;
$distance = sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat)) + cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta));
$distance = acos($distance);
$distance = abs(rad2deg($distance));
// echo '<br />'.$timezone_id.' '.$distance;
if (!$time_zone || $tz_distance > $distance) {
$time_zone = $timezone_id;
$tz_distance = $distance;
}
}
}
return $time_zone;
}
return 'unknown';
}
示例4: timezones_from_countryCode
function timezones_from_countryCode($country_code, $country_name)
{
$dt = new DateTime();
// create a list of timezones based on that country code..
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code);
$timezone_offset = [];
// instantiate timezone_offset array
foreach ($timezones as $timezone) {
$tz = new DateTimeZone($timezone);
$timezone_offset[$timezone] = $tz->getOffset(new DateTime());
}
// sort by offset
asort($timezone_offset);
// format display of timezone and offset
foreach ($timezone_offset as $raw_timezone => $offset) {
$dt->setTimezone(new DateTimeZone($raw_timezone));
$timezone_abbr = $dt->format('T');
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate('H:i', abs($offset));
$pretty_offset = "UTC{$offset_prefix}{$offset_formatted}";
// if( ($pos = strpos($raw_timezone, '/') ) !== false ) { // remove 'America/'
// $clean_timezone = substr($raw_timezone, $pos+1);
// if( ($pos = strpos($clean_timezone, '/')) !== false ) { // remove second level '.../'
// $clean_timezone = substr($clean_timezone, $pos+1);
// }
// }
// $clean_timezone = str_replace('_',' ',$clean_timezone); // remove the '_' in city names
$clean_timezone = User::clean_city($raw_timezone);
echo "<option value=\"{$raw_timezone}\">(" . $pretty_offset . ") " . $clean_timezone . ' (' . $timezone_abbr . ')</option>';
}
}
示例5: _timezone
protected function _timezone()
{
$element = new Zend_Form_Element_Select('timezone');
$element->setLabel('Timezone')->addDecorators($this->_decorators)->setRequired(true)->setAttrib('class', 'span4');
$timezones = array();
$timezoneIdentifiers = DateTimeZone::listIdentifiers();
foreach ($timezoneIdentifiers as $timezone) {
if (preg_match('/^(Africa|America|Antarctica|Asia|Atlantic|Europe|Indian|Pacific)\\//', $timezone)) {
$ex = explode('/', $timezone);
$city = isset($ex[2]) ? $ex[1] . ' - ' . $ex[2] : $ex[1];
$name = $ex[0];
$timezones[$name][$timezone] = $city;
$dateTimeZoneGmt = new DateTimeZone('GMT');
$dateTimeZone = new DateTimeZone($timezone);
$dateTimeGmt = new DateTime("now", $dateTimeZoneGmt);
$timeOffset = $dateTimeZone->getOffset($dateTimeGmt);
$gmt = $timeOffset / 3600;
if ($gmt == 0) {
$gmt = ' 00';
} elseif ($gmt > 0 && $gmt < 10) {
$gmt = '+0' . $gmt;
} elseif ($gmt >= 10) {
$gmt = '+' . $gmt;
} elseif ($gmt < 0 && $gmt > -10) {
$gmt = '-0' . abs($gmt);
}
$timezones[$name][$timezone] = substr($timezone, strlen($name) + 1) . ' (GMT ' . $gmt . ':00)';
}
}
$element->addMultiOptions($timezones);
return $element;
}
示例6: __construct
public function __construct($tqx, $tq, $tqrt, $tz, $locale, $extra = NULL)
{
$this->response = array('status' => 'ok');
$this->params = array('version' => '0.6', 'sig' => '', 'responseHandler' => 'google.visualization.Query.setResponse');
if ($extra) {
foreach ($extra as $key => $value) {
$this->params[$key] = $value;
}
}
if ($tqx) {
foreach (explode(';', $tqx) as $kvpair) {
$kva = explode(':', $kvpair, 2);
if (count($kva) == 2) {
$this->params[$kva[0]] = $kva[1];
}
}
}
if (get_magic_quotes_gpc()) {
$tq = stripslashes($tq);
}
$this->debug = $extra && $extra['debug'];
$this->tq = $tq;
$this->tqrt = $tqrt;
$this->tz = $tz;
$this->locale = $locale;
$timezone = new DateTimeZone($tz);
$date = new DateTime("", $timezone);
$this->gmt_offset = $timezone->getOffset($date);
}
示例7: generate_timezone_list
function generate_timezone_list()
{
static $regions = array(DateTimeZone::AFRICA, DateTimeZone::AMERICA, DateTimeZone::ANTARCTICA, DateTimeZone::ASIA, DateTimeZone::ATLANTIC, DateTimeZone::AUSTRALIA, DateTimeZone::EUROPE, DateTimeZone::INDIAN, DateTimeZone::PACIFIC);
$timezones = array();
foreach ($regions as $region) {
$timezones = array_merge($timezones, DateTimeZone::listIdentifiers($region));
}
$timezone_offsets = array();
foreach ($timezones as $timezone) {
$tz = new DateTimeZone($timezone);
$timezone_offsets[$timezone] = $tz->getOffset(new DateTime());
}
// sort timezone by timezone name
ksort($timezone_offsets);
$timezone_list = array();
foreach ($timezone_offsets as $timezone => $offset) {
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate('H:i', abs($offset));
$pretty_offset = "UTC{$offset_prefix}{$offset_formatted}";
$t = new DateTimeZone($timezone);
$c = new DateTime(null, $t);
$current_time = $c->format('g:i A');
$timezone_list["({$pretty_offset}) {$timezone}"] = "{$timezone} - {$current_time} ({$pretty_offset})";
}
return $timezone_list;
}
示例8: offset
/**
* Returns the offset (in seconds) between two time zones.
* @see http://php.net/timezones
*
* @param string timezone that to find the offset of
* @param string|boolean timezone used as the baseline
* @return integer
*/
public static function offset($remote, $local = YES, $alt_format = false)
{
static $offsets;
// Default values
$remote = (string) $remote;
$local = $local === YES ? date_default_timezone_get() : (string) $local;
// Cache key name
$cache = $remote . $local;
if (empty($offsets[$cache])) {
// Create timezone objects
$remote = new DateTimeZone($remote);
$local = new DateTimeZone($local);
// Create date objects from timezones
$time_there = new DateTime('now', $remote);
$time_here = new DateTime('now', $local);
// Find the offset
$offset = $remote->getOffset($time_there) - $local->getOffset($time_here);
// Return offset in +3:00 format
if ($alt_format) {
$offset = $offset / 60;
$hours = floor($offset / 60);
$min = str_pad(abs($offset % 60), 2, 0);
if ($hours > 0) {
$hours = '+' . $hours;
}
$offsets[$cache] = $hours . ':' . $min;
} else {
$offsets[$cache] = $offset;
}
}
return $offsets[$cache];
}
示例9: getServerTimeZoneInt
/**
* Return server timezone int.
*
* @param string $refgmtdate Reference period for timezone (timezone differs on winter and summer. May be 'now', 'winter' or 'summer')
* @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer)
*/
function getServerTimeZoneInt($refgmtdate = 'now')
{
global $conf;
if (method_exists('DateTimeZone', 'getOffset')) {
// Method 1 (include daylight)
$gmtnow = dol_now('gmt');
$yearref = dol_print_date($gmtnow, '%Y');
$monthref = dol_print_date($gmtnow, '%m');
$dayref = dol_print_date($gmtnow, '%d');
if ($refgmtdate == 'now') {
$newrefgmtdate = $yearref . '-' . $monthref . '-' . $dayref;
} elseif ($refgmtdate == 'summer') {
$newrefgmtdate = $yearref . '-08-01';
} else {
$newrefgmtdate = $yearref . '-01-01';
}
$newrefgmtdate .= 'T00:00:00+00:00';
$localtz = new DateTimeZone(getServerTimeZoneString());
$localdt = new DateTime($newrefgmtdate, $localtz);
$tmp = -1 * $localtz->getOffset($localdt);
//print $refgmtdate.'='.$tmp;
} else {
$tmp = 0;
dol_print_error('', 'PHP version must be 5.3+');
}
$tz = round(($tmp < 0 ? 1 : -1) * abs($tmp / 3600));
return $tz;
}
示例10: actionGeneralSettings
/**
* Shows the general settings form.
*
* @param array $variables
*/
public function actionGeneralSettings(array $variables = array())
{
if (empty($variables['info'])) {
$variables['info'] = Craft::getInfo();
}
// Assemble the timezone options array
// (Technique adapted from http://stackoverflow.com/a/7022536/1688568)
$variables['timezoneOptions'] = array();
$utc = new DateTime();
$offsets = array();
$timezoneIds = array();
$includedAbbrs = array();
foreach (\DateTimeZone::listIdentifiers() as $timezoneId) {
$timezone = new \DateTimeZone($timezoneId);
$transition = $timezone->getTransitions($utc->getTimestamp(), $utc->getTimestamp());
$abbr = $transition[0]['abbr'];
$offset = round($timezone->getOffset($utc) / 60);
if ($offset) {
$hour = floor($offset / 60);
$minutes = floor(abs($offset) % 60);
$format = sprintf('%+d', $hour);
if ($minutes) {
$format .= ':' . sprintf('%02u', $minutes);
}
} else {
$format = '';
}
$offsets[] = $offset;
$timezoneIds[] = $timezoneId;
$includedAbbrs[] = $abbr;
$variables['timezoneOptions'][$timezoneId] = 'UTC' . $format . ($abbr != 'UTC' ? " ({$abbr})" : '') . ($timezoneId != 'UTC' ? ' - ' . $timezoneId : '');
}
array_multisort($offsets, $timezoneIds, $variables['timezoneOptions']);
$this->renderTemplate('settings/general/index', $variables);
}
示例11: getTimezoneData
private static function getTimezoneData($timezone_identifier)
{
// I'm very shame of that code but now i cannot create something better due
// php hasn't a smart API (even after they have
// rewrote datetime functions).
// Here we should find the offset for the provided timezone and determine
// whether the timezone has the DST offset ever.
// We cannot use date("I") due it depends on current time (if the current time
// is in winter, we 'll skip the zone)
// so the best safe (but not fast) practice is to scan all tz transitions
$timezone = new DateTimeZone($timezone_identifier);
$current_date = new DateTime("now", $timezone);
$current_ts = strtotime($current_date->format(DATE_W3C));
$data = array("offset" => 0, "dst" => false);
$transitions = $timezone->getTransitions();
foreach ($transitions as &$transition) {
$tt = new DateTime($transition['time'], $timezone);
$ts = strtotime($tt->format(DATE_W3C));
if ($ts < $current_ts) {
continue;
}
if (!$transition['isdst']) {
$transition = next($transitions);
}
$data['dst'] = $transition['isdst'];
$data['offset'] = self::makeOffset($transition);
break;
}
return $data;
}
示例12: generateTimezoneList
function generateTimezoneList()
{
$regions = array(DateTimeZone::AFRICA, DateTimeZone::AMERICA, DateTimeZone::ANTARCTICA, DateTimeZone::ASIA, DateTimeZone::ATLANTIC, DateTimeZone::AUSTRALIA, DateTimeZone::EUROPE, DateTimeZone::INDIAN, DateTimeZone::PACIFIC);
$timezones = [];
foreach ($regions as $region) {
$timezones = array_merge($timezones, DateTimeZone::listIdentifiers($region));
}
$timezone_offsets = [];
foreach ($timezones as $timezone) {
$tz = new DateTimeZone($timezone);
$utc = new DateTimeZone('UTC');
$timezone_offsets[$timezone] = $tz->getOffset(new DateTime('now', $utc));
}
// sort timezone by timezone name
ksort($timezone_offsets);
$timezone_list = [];
foreach ($timezone_offsets as $timezone => $offset) {
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate('H:i', abs($offset));
$pretty_offset = "UTC{$offset_prefix}{$offset_formatted}";
$split = explode("/", $timezone);
$timezone_list[$timezone] = "{$split['1']}/{$split['0']} ({$pretty_offset})";
}
asort($timezone_list);
return $timezone_list;
}
示例13: generate_timezone_list
function generate_timezone_list()
{
$the_list = array();
static $regions = array(DateTimeZone::AFRICA, DateTimeZone::AMERICA, DateTimeZone::ANTARCTICA, DateTimeZone::ASIA, DateTimeZone::ATLANTIC, DateTimeZone::AUSTRALIA, DateTimeZone::EUROPE, DateTimeZone::INDIAN, DateTimeZone::PACIFIC);
$timezones = array();
foreach ($regions as $region) {
$timezones = array_merge($timezones, DateTimeZone::listIdentifiers($region));
}
$timezone_offsets = array();
foreach ($timezones as $timezone) {
$tz = new DateTimeZone($timezone);
$timezone_offsets[$timezone] = $tz->getOffset(new DateTime());
}
// sort timezone by timezone name
ksort($timezone_offsets);
$timezone_list = array();
foreach ($timezone_offsets as $timezone => $offset) {
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate('H:i', abs($offset));
$pretty_offset = "UTC{$offset_prefix}{$offset_formatted}";
$t = new DateTimeZone($timezone);
$c = new DateTime(null, $t);
$current_time = $c->format('g:i A');
$zone_abbrev = $c->format('T');
$timezone_list[$timezone] = "({$zone_abbrev}) ({$pretty_offset}) {$timezone} - {$current_time}";
$the_list_item['display'] = $timezone_list[$timezone];
$the_list_item['offset'] = $pretty_offset;
$the_list_item['zone'] = $timezone;
$the_list_item['abbrev'] = $zone_abbrev . '(' . $pretty_offset . ')' . $timezone;
array_push($the_list, $the_list_item);
}
return $the_list;
}
示例14: getList
/**
* Get a list of localized timezone names
*
* @return array
*/
public static function getList()
{
$xoops = \Xoops::getInstance();
$locale = \Xoops\Locale::getCurrent();
$key = ['system', 'lists', 'timezone', $locale];
//$xoops->cache()->delete($key);
$timeZones = $xoops->cache()->cacheRead($key, function () {
$timeZones = array();
$territories = Territory::getContinentsAndCountries();
$maxLen = 0;
$utcDtz = new \DateTimeZone('UTC');
foreach ($territories as $byContinent) {
$continent = $byContinent['name'];
foreach ($byContinent['children'] as $cCode => $cName) {
$allZones = $utcDtz->listIdentifiers(\DateTimeZone::PER_COUNTRY, $cCode);
foreach ($allZones as $zone) {
$maxLen = max(strlen($zone), $maxLen);
$name = Calendar::getTimezoneExemplarCity($zone);
if (!isset($timeZones[$zone]) && !empty($name)) {
$timeZones[$zone] = $continent . '/' . $name;
}
}
}
}
\XoopsLocale::asort($timeZones);
$default = array('UTC' => Calendar::getTimezoneNameNoLocationSpecific(new \DateTimeZone('GMT')));
$timeZones = array_merge($default, $timeZones);
return $timeZones;
});
return $timeZones;
}
示例15: _getEvtTime
/**
* Find the local time at the event...
* @param $evt_offset integer Event's offset from UTC
*/
private function _getEvtTime($evt_offset)
{
$here = new DateTimeZone(date_default_timezone_get());
$hoffset = $here->getOffset(new DateTime("now", $here));
$off = time() - $hoffset + $evt_offset * 3600;
return $off;
}