本文整理汇总了PHP中DateTimeZone::getTransitions方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTimeZone::getTransitions方法的具体用法?PHP DateTimeZone::getTransitions怎么用?PHP DateTimeZone::getTransitions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeZone
的用法示例。
在下文中一共展示了DateTimeZone::getTransitions方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: DateTimeZone
function render_field($field)
{
/*
* Create a select dropdown with all available timezones
*/
$utc = new DateTimeZone('UTC');
$dt = new DateTime('now', $utc);
?>
<select name="<?php
echo esc_attr($field['name']);
?>
">
<?php
foreach (\DateTimeZone::listIdentifiers() as $tz) {
$current_tz = new \DateTimeZone($tz);
$transition = $current_tz->getTransitions($dt->getTimestamp(), $dt->getTimestamp());
$abbr = $transition[0]['abbr'];
$is_selected = trim($field['value']) === trim($tz) ? ' selected="selected"' : '';
?>
<option value="<?php
echo $tz;
?>
"<?php
echo $is_selected;
?>
><?php
echo $tz . ' (' . $abbr . ')';
?>
</option>
<?php
}
?>
</select>
<?php
}
示例3: 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);
}
示例4: tzdata
/**
Return additional information for specified Unix timezone
@return array
@param $id string
@private
**/
private static function tzdata($id)
{
$ref = new DateTimeZone($id);
$loc = $ref->getLocation();
$now = time();
$trn = $ref->getTransitions($now, $now);
return array('offset' => $ref->getOffset(new DateTime('now', new DateTimeZone('GMT'))) / 3600, 'country' => $loc['country_code'], 'latitude' => $loc['latitude'], 'longitude' => $loc['longitude'], 'dst' => $trn[0]['isdst']);
}
示例5: tzinfo
/**
* Return information about specified Unix time zone
* @return array
* @param $zone string
**/
function tzinfo($zone)
{
$ref = new \DateTimeZone($zone);
$loc = $ref->getLocation();
$trn = $ref->getTransitions($now = time(), $now);
$out = array('offset' => $ref->getOffset(new \DateTime('now', new \DateTimeZone('GMT'))) / 3600, 'country' => $loc['country_code'], 'latitude' => $loc['latitude'], 'longitude' => $loc['longitude'], 'dst' => $trn[0]['isdst']);
unset($ref);
return $out;
}
示例6: timezoneDoesDST
function timezoneDoesDST($tzId, $time = "")
{
$tz = new DateTimeZone($tzId);
$date = new DateTime($time != "" ? $time : "now", $tz);
$trans = $tz->getTransitions();
foreach ($trans as $k => $t) {
if ($t["ts"] > $date->format('U')) {
return $trans[$k - 1]['isdst'];
}
}
}
示例7: setTimezone
/**
* Verifies that the given timezone exists and sets the timezone to the selected timezone
*
* @TODO Verify that the given timezone exists
*
* @param string $timezoneName
* @return string
*/
public function setTimezone($timeZoneName = 'UTC')
{
if (!$this->isValidTimeZone($timeZoneName)) {
$timeZoneName = 'UTC';
}
$this->timezone = new \DateTimeZone($timeZoneName);
$this->timezoneId = $this->timezone->getName();
$transitions = $this->timezone->getTransitions();
$this->timezoneInDST = $transitions[0]['isdst'];
return $this->timezoneId;
}
示例8: timezoneExhibitsDST
protected function timezoneExhibitsDST($tzId)
{
$tz = new \DateTimeZone($tzId);
$date = new \DateTime("now", $tz);
$trans = $tz->getTransitions();
foreach ($trans as $k => $t) {
if ($t["ts"] > $date->format('U')) {
return $trans[$k - 1]['isdst'];
}
}
return false;
}
示例9: __construct
/**
* Creates a new component.
*
* By default this object will iterate over its own children, but this can
* be overridden with the iterator argument
*
* @param string $name
* @param Sabre\VObject\ElementList $iterator
*/
public function __construct()
{
parent::__construct();
$tz = new \DateTimeZone(\GO::user() ? \GO::user()->timezone : date_default_timezone_get());
//$tz = new \DateTimeZone("Europe/Amsterdam");
$transitions = $tz->getTransitions();
$start_of_year = mktime(0, 0, 0, 1, 1);
$to = \GO\Base\Util\Date::get_timezone_offset(time());
if ($to < 0) {
if (strlen($to) == 2) {
$to = '-0' . $to * -1;
}
} else {
if (strlen($to) == 1) {
$to = '0' . $to;
}
$to = '+' . $to;
}
$STANDARD_TZOFFSETFROM = $STANDARD_TZOFFSETTO = $DAYLIGHT_TZOFFSETFROM = $DAYLIGHT_TZOFFSETTO = $to;
$STANDARD_RRULE = '';
$DAYLIGHT_RRULE = '';
for ($i = 0, $max = count($transitions); $i < $max; $i++) {
if ($transitions[$i]['ts'] > $start_of_year) {
$weekday1 = $this->_getDay($transitions[$i]['time']);
$weekday2 = $this->_getDay($transitions[$i + 1]['time']);
if ($transitions[$i]['isdst']) {
$dst_start = $transitions[$i];
$dst_end = $transitions[$i + 1];
} else {
$dst_end = $transitions[$i];
$dst_start = $transitions[$i + 1];
}
$STANDARD_TZOFFSETFROM = $this->_formatVtimezoneTransitionHour($dst_start['offset'] / 3600);
$STANDARD_TZOFFSETTO = $this->_formatVtimezoneTransitionHour($dst_end['offset'] / 3600);
$DAYLIGHT_TZOFFSETFROM = $this->_formatVtimezoneTransitionHour($dst_end['offset'] / 3600);
$DAYLIGHT_TZOFFSETTO = $this->_formatVtimezoneTransitionHour($dst_start['offset'] / 3600);
$DAYLIGHT_RRULE = "FREQ=YEARLY;BYDAY={$weekday1};BYMONTH=" . date('n', $dst_start['ts']);
$STANDARD_RRULE = "FREQ=YEARLY;BYDAY={$weekday2};BYMONTH=" . date('n', $dst_end['ts']);
break;
}
}
$this->tzid = $tz->getName();
// $this->add("last-modified", "19870101T000000Z");
$rrule = new \Sabre\VObject\Recur\RRuleIterator($STANDARD_RRULE, new \DateTime('1970-01-01 ' . substr($STANDARD_TZOFFSETFROM, 1) . ':00'));
$rrule->next();
$rrule->next();
$this->add($this->createComponent("standard", array('dtstart' => $rrule->current()->format('Ymd\\THis'), 'rrule' => $STANDARD_RRULE, 'tzoffsetfrom' => $STANDARD_TZOFFSETFROM . "00", 'tzoffsetto' => $STANDARD_TZOFFSETTO . "00")));
$rrule = new \Sabre\VObject\Recur\RRuleIterator($DAYLIGHT_RRULE, new \DateTime('1970-01-01 ' . substr($DAYLIGHT_TZOFFSETFROM, 1) . ':00'));
$rrule->next();
$rrule->next();
$this->add($this->createComponent("daylight", array('dtstart' => $rrule->current()->format('Ymd\\THis'), 'rrule' => $DAYLIGHT_RRULE, 'tzoffsetfrom' => $DAYLIGHT_TZOFFSETFROM . "00", 'tzoffsetto' => $DAYLIGHT_TZOFFSETTO . "00")));
}
示例10: getInfo
public static function getInfo()
{
static $tz_info;
if (!$tz_info) {
$tz = Customization::get('timezone');
$utc = new \DateTimeZone('UTC');
$dt = new \DateTime('now', $utc);
$current_tz = new \DateTimeZone($tz);
$offset = $current_tz->getOffset($dt);
$transition = $current_tz->getTransitions($dt->getTimestamp(), $dt->getTimestamp());
$dt_in_tz = new \DateTime('now', $current_tz);
$tz_info = array('code' => $tz, 'gmt_offset_seconds' => (double) $offset, 'gmt_offset_hours' => (double) ($offset / 3600), 'name' => $transition[0]['name'], 'abbr' => $transition[0]['abbr'], 'tz_object' => $current_tz, 'utc_object' => $utc, 'now_utc' => $dt, 'now' => $dt_in_tz);
}
return $tz_info;
}
示例11: getTimezonesAsArray
/** Returns an associative array with all the timezones as keys and a friendly description of each of them as values */
public static function getTimezonesAsArray()
{
$utc = new \DateTimeZone('UTC');
$dt = new \DateTime('now', $utc);
$result = array();
foreach (\DateTimeZone::listIdentifiers() as $tz) {
$current_tz = new \DateTimeZone($tz);
$offset = $current_tz->getOffset($dt);
$transition = $current_tz->getTransitions($dt->getTimestamp(), $dt->getTimestamp());
$abbr = $transition[0]['abbr'];
$formatted = sprintf('%+03d:%02u', floor($offset / 3600), floor(abs($offset) % 3600 / 60));
$result[$tz] = "{$tz} [ {$abbr} {$formatted} ]";
}
return $result;
}
示例12: day_of
function day_of($time, $tzid=NULL) {
$tztime = $time - ($time % 86400);
if ($tzid !== NULL) {
$timezone = new DateTimeZone($tzid);
$transitions = $timezone->getTransitions();
$is_dst = date('I', $tztime);
$offset = 0;
foreach ($transitions as $transition) {
if ($transition['isdst'] == $is_dst) {
$offset = $transition['offset'];
break;
}
}
$tztime = $tztime - $offset;
if ($tztime > $time)
$tztime -= 86400;
}
return $tztime;
}
示例13: vtimezone
private function vtimezone($icalEvents)
{
$params = JComponentHelper::getParams(JEV_COM_COMPONENT);
$tzid = "";
if (is_callable("date_default_timezone_set")) {
$current_timezone = date_default_timezone_get();
// Do the Timezone definition
$tzid = ";TZID={$current_timezone}";
// find the earliest start date
$firststart = false;
foreach ($icalEvents as $a) {
if (!$firststart || $a->getUnixStartTime() < $firststart) {
$firststart = $a->getUnixStartTime();
}
}
// Subtract 1 leap year to make sure we have enough transitions
$firststart -= 31622400;
$timezone = new DateTimeZone($current_timezone);
if (version_compare(PHP_VERSION, "5.3.0") >= 0) {
$transitions = $timezone->getTransitions($firststart);
} else {
$transitions = $timezone->getTransitions();
}
$tzindex = 0;
while (JevDate::strtotime($transitions[$tzindex]['time']) < $firststart) {
$tzindex++;
}
$transitions = array_slice($transitions, $tzindex);
if (count($transitions) >= 2) {
$lastyear = $params->get("com_latestyear", 2020);
echo "BEGIN:VTIMEZONE\n";
echo "TZID:{$current_timezone}\n";
for ($t = 0; $t < count($transitions); $t++) {
$transition = $transitions[$t];
if ($transition['isdst'] == 0) {
if (JevDate::strftime("%Y", $transition['ts']) > $lastyear) {
continue;
}
echo "BEGIN:STANDARD\n";
echo "DTSTART:" . JevDate::strftime("%Y%m%dT%H%M%S\n", $transition['ts']);
if ($t < count($transitions) - 1) {
echo "RDATE:" . JevDate::strftime("%Y%m%dT%H%M%S\n", $transitions[$t + 1]['ts']);
}
// if its the first transition then assume the old setting is the same as the next otherwise use the previous value
$prev = $t;
$prev += $t == 0 ? 1 : -1;
$offset = $transitions[$prev]["offset"];
$sign = $offset >= 0 ? "+" : "-";
$offset = abs($offset);
$offset = $sign . sprintf("%04s", floor($offset / 3600) * 100 + $offset % 60);
echo "TZOFFSETFROM:{$offset}\n";
$offset = $transitions[$t]["offset"];
$sign = $offset >= 0 ? "" : "-";
$offset = abs($offset);
$offset = $sign . sprintf("%04s", floor($offset / 3600) * 100 + $offset % 60);
echo "TZOFFSETTO:{$offset}\n";
echo "TZNAME:{$current_timezone} " . $transitions[$t]["abbr"] . "\n";
echo "END:STANDARD\n";
}
}
for ($t = 0; $t < count($transitions); $t++) {
$transition = $transitions[$t];
if ($transition['isdst'] == 1) {
if (JevDate::strftime("%Y", $transition['ts']) > $lastyear) {
continue;
}
echo "BEGIN:DAYLIGHT\n";
echo "DTSTART:" . JevDate::strftime("%Y%m%dT%H%M%S\n", $transition['ts']);
if ($t < count($transitions) - 1) {
echo "RDATE:" . JevDate::strftime("%Y%m%dT%H%M%S\n", $transitions[$t + 1]['ts']);
}
// if its the first transition then assume the old setting is the same as the next otherwise use the previous value
$prev = $t;
$prev += $t == 0 ? 1 : -1;
$offset = $transitions[$prev]["offset"];
$sign = $offset >= 0 ? "+" : "-";
$offset = abs($offset);
$offset = $sign . sprintf("%04s", floor($offset / 3600) * 100 + $offset % 60);
echo "TZOFFSETFROM:{$offset}\n";
$offset = $transitions[$t]["offset"];
$sign = $offset >= 0 ? "" : "-";
$offset = abs($offset);
$offset = $sign . sprintf("%04s", floor($offset / 3600) * 100 + $offset % 60);
echo "TZOFFSETTO:{$offset}\n";
echo "TZNAME:{$current_timezone} " . $transitions[$t]["abbr"] . "\n";
echo "END:DAYLIGHT\n";
}
}
echo "END:VTIMEZONE\n";
}
}
return $tzid;
}
示例14: _getTZOffsetTransitions
/**
* Retrieve transitions for offsets of given timezone
*
* @param string $timezone
* @param mixed $from
* @param mixed $to
* @return array
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _getTZOffsetTransitions($timezone, $from = null, $to = null)
{
$tzTransitions = [];
try {
if (!empty($from)) {
$from = $from instanceof \DateTime
? $from->getTimestamp()
: (new \DateTime($from))->getTimestamp();
}
$to = $to instanceof \DateTime
? $to
: new \DateTime($to);
$nextPeriod = $this->getConnection()->formatDate(
$to->format('Y-m-d H:i:s')
);
$to = $to->getTimestamp();
$dtz = new \DateTimeZone($timezone);
$transitions = $dtz->getTransitions();
for ($i = count($transitions) - 1; $i >= 0; $i--) {
$tr = $transitions[$i];
try {
$this->timezoneValidator->validate($tr['ts'], $to);
} catch (\Magento\Framework\Exception\ValidatorException $e) {
continue;
}
$tr['time'] = $this->getConnection()->formatDate(
(new \DateTime($tr['time']))->format('Y-m-d H:i:s')
);
$tzTransitions[$tr['offset']][] = ['from' => $tr['time'], 'to' => $nextPeriod];
if (!empty($from) && $tr['ts'] < $from) {
break;
}
$nextPeriod = $tr['time'];
}
} catch (\Exception $e) {
$this->_logger->critical($e);
}
return $tzTransitions;
}
示例15: DateTimeZone
var_dump(timezone_name_get($tz));
// Create two timezone objects, one for Taipei (Taiwan) and one for
// Tokyo (Japan)
$dateTimeZoneTaipei = timezone_open("Asia/Taipei");
$dateTimeZoneJapan = timezone_open("Asia/Tokyo");
// Create two DateTime objects that will contain the same Unix timestamp, but
// have different timezones attached to them.
$dateTimeTaipei = date_create("2008-08-08", $dateTimeZoneTaipei);
$dateTimeJapan = date_create("2008-08-08", $dateTimeZoneJapan);
var_dump(date_offset_get($dateTimeTaipei));
var_dump(date_offset_get($dateTimeJapan));
$tz = timezone_open("Asia/Shanghai");
var_dump(timezone_name_get($tz));
$timezone = timezone_open("CET");
$transitions = timezone_transitions_get($timezone);
var_dump($transitions[0]['ts']);
var_dump($transitions[0]['offset']);
var_dump($transitions[0]['isdst']);
var_dump($transitions[0]['abbr']);
$tz = timezone_open("EDT");
var_dump(timezone_name_get($tz));
$tz = timezone_open("PST");
var_dump(timezone_name_get($tz));
$tz = timezone_open("CHAST");
var_dump(timezone_name_get($tz));
var_dump((bool) timezone_version_get());
$timezone = new DateTimeZone("Europe/London");
$transitions = $timezone->getTransitions(1000000, 999999999);
var_dump($transitions[0]);
var_dump($transitions[3]);
var_dump($transitions[count($transitions) - 1]);