本文整理汇总了PHP中DateTime::getTimezone方法的典型用法代码示例。如果您正苦于以下问题:PHP DateTime::getTimezone方法的具体用法?PHP DateTime::getTimezone怎么用?PHP DateTime::getTimezone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTime
的用法示例。
在下文中一共展示了DateTime::getTimezone方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getChangeTime
/**
*
* Possible bug was also detected: date_sunrise() seems to return date of the previous day:
* https://bugs.php.net/bug.php?id=53148
*
* To avoid issues, only compare day agnostic formats: $dt->format('Hi')
*
* @param \DateTime $dateTime
* @param $method
*
* @return \DateTime
*/
private function getChangeTime(\DateTime $dateTime, $method)
{
$location = $dateTime->getTimezone()->getLocation();
$horizonShift = new \DateTime('now', $dateTime->getTimezone());
$horizonShift->setTimestamp($method($dateTime->getTimestamp(), SUNFUNCS_RET_TIMESTAMP, $location['latitude'], $location['longitude'], ini_get("date.sunrise_zenith"), $dateTime->getOffset() / 3600));
return $horizonShift;
}
示例2: testSetTimezone
/**
*
*/
public function testSetTimezone()
{
$dt = new DateTime('2013-03-08 10:00:00');
$dt->setTimezone('Europe/Berlin');
$this->assertEquals('2013-03-08 11:00:00', $dt->format('Y-m-d H:i:s'));
$this->assertEquals('Europe/Berlin', $dt->getTimezone()->getName());
$dt->setTimezone(new \DateTimeZone('Indian/Mahe'), true);
$this->assertEquals('2013-03-08 11:00:00', $dt->format('Y-m-d H:i:s'));
$this->assertEquals('Indian/Mahe', $dt->getTimezone()->getName());
$dt->setTimezone(null);
$this->assertEquals('UTC', $dt->getTimezone()->getName());
}
示例3: toString
public function toString(\DateTime $pDateTime, $pDateTimeZone)
{
if ($pDateTimeZone->getName() == $pDateTime->getTimezone()->getName()) {
return $pDateTime->format('c');
} else {
$lDateTimeZone = $pDateTime->getTimezone();
$pDateTime->setTimezone($pDateTimeZone);
$lDateTimeString = $pDateTime->format('c');
$pDateTime->setTimezone($lDateTimeZone);
return $lDateTimeString;
}
return $pDateTime->format('c');
}
示例4: format
/**
* @param $format
* @return bool|string
*/
public function format($format)
{
// convert alias string
if (in_array($format, array_keys($this->formats))) {
$format = $this->formats[$format];
}
// if valid unix timestamp...
if ($this->dateTime !== false) {
return jDateTime::strftime($format, $this->dateTime->getTimestamp(), $this->dateTime->getTimezone());
} else {
return false;
}
}
示例5: getDateformat
/**
* getDateformat
* @param string $dateFormat
* @param string $timeFormat
* @param string $locale
* @param string $timezone
* @param string $format
* @return string
**/
public function getDateformat($dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null)
{
$date = new \DateTime();
$formatValues = array('none' => IntlDateFormatter::NONE, 'short' => IntlDateFormatter::SHORT, 'medium' => IntlDateFormatter::MEDIUM, 'long' => IntlDateFormatter::LONG, 'full' => IntlDateFormatter::FULL);
$formatter = IntlDateFormatter::create($locale, $formatValues[$dateFormat], $formatValues[$timeFormat], $date->getTimezone()->getName(), IntlDateFormatter::GREGORIAN, $format);
return strtoupper($formatter->getPattern());
}
示例6: getTimezoneSelectOptions
public static function getTimezoneSelectOptions()
{
$dateTimeObject = new \DateTime();
$currentTimeZone = isset($_SESSION['config']) && !empty($_SESSION['config']['timezone']) ? $_SESSION['config']['timezone'] : $dateTimeObject->getTimezone()->getName();
$timezoneSelectOptions = '';
$timezones = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC);
$lastGroup = '';
foreach ($timezones as $timezone) {
$timezoneParts = explode('/', $timezone);
$curGroup = $timezoneParts[0];
if ($curGroup != $lastGroup) {
if ($lastGroup != '') {
$timezoneSelectOptions .= '</optgroup>';
}
$timezoneSelectOptions .= '<optgroup label="' . addslashes($curGroup) . '">';
$lastGroup = $curGroup;
}
if ($timezone == $currentTimeZone) {
$selected = ' selected="selected"';
} else {
$selected = '';
}
$timezoneSelectOptions .= '<option' . $selected . ' value="' . addslashes($timezone) . '">' . htmlspecialchars($timezone) . '</option>';
}
return $timezoneSelectOptions;
}
示例7: main
function main()
{
$date = new DateTime("now");
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
$tz = $date->getTimezone();
print $tz->getName() . "\n";
}
示例8: __construct
/**
* Returns new DateTime object
*
* @param string|int|DateTime $_time
* @param string|DateTimeZone $_timezone
*/
public function __construct($_time = "now", $_timezone = NULL)
{
// allow to pass instanceof DateTime
if ($_time instanceof DateTime) {
if (!$_timezone) {
$_timezone = $_time->getTimezone();
} else {
$_time = clone $_time;
$_time->setTimezone($_timezone);
}
$time = $_time->format("Y-m-d H:i:s");
} else {
$time = is_numeric($_time) ? "@" . floor($_time) : $_time;
}
if ($_timezone) {
if (!$_timezone instanceof DateTimeZone) {
$_timezone = new DateTimeZone($_timezone);
}
parent::__construct($time, $_timezone);
} else {
parent::__construct($time);
}
// Normalize Timezonename, as sometimes +00:00 is taken
if (is_numeric($_time)) {
$this->setTimezone('UTC');
}
}
示例9: parseDate
/**
* convert a string into the required date format for the database
* important: returned date is in german local time (mez)!
* @param $string String the given string
* @return string|null
*/
public function parseDate($string)
{
$localTime = new DateTime();
$date = strtotime($string);
// format yyyy-mm-ddThh:ii:ssZ
if ('z' === strpos(strtolower($string), -1, 1)) {
$string = strtolower($string);
$string = str_replace('z', '', $string);
$string = str_replace('t', ' ', $string);
$date = new DateTime($string, DateTimeZone::UTC);
$date->setTimezone($localTime->getTimezone());
$date = $date->format('Y-m-d H:i:s');
} else {
if ($date) {
$date = date('Y-m-d H:i:s', $date);
$date = new DateTime($date);
$date = $date->format('Y-m-d H:i:s');
}
}
if ($date && date('Y-m-d H:i:s') > $date) {
return $date;
}
// if the date can not be parsed or is in future return the current date
return date('Y-m-d H:i:s');
}
示例10: mm_countdown
/**
* Build and return the Countdown component.
*
* @since 1.0.0
*
* @param array $args The args.
*
* @return string The HTML.
*/
function mm_countdown($args)
{
$component = 'mm-countdown';
// Set our defaults and use them as needed.
$defaults = array('date' => '', 'time' => '', 'timezone' => '');
$args = wp_parse_args((array) $args, $defaults);
// Get clean param values.
$date = $args['date'];
$time = $args['time'];
$timezone = $args['timezone'];
// Enqueue pre-registerd 3rd party countdown script.
wp_enqueue_script('mm-jquery-countdown');
// Get Mm classes.
$mm_classes = apply_filters('mm_components_custom_classes', '', $component, $args);
// Create new date object.
$date_obj = new DateTime($date . ' ' . $time . ' ' . $timezone);
$utc_date_ojb = new DateTime($date . ' ' . $time);
// Get timezone offset.
$timezone_offset = $date_obj->getTimezone()->getOffset($utc_date_ojb) / 3600;
// Pass data to JS as data attributes.
$year = $date_obj->format('Y');
$month = $date_obj->format('n');
$day = $date_obj->format('j');
$hour = $date_obj->format('G');
$minute = $date_obj->format('i');
$second = $date_obj->format('s');
return sprintf('<div class="%s" data-year="%s" data-month="%s" data-day="%s" data-hour="%s" data-minute="%s" data-second="%s" data-timezone-offset="%s"></div>', esc_attr($mm_classes), esc_attr($year), esc_attr($month), esc_attr($day), esc_attr($hour), esc_attr($minute), esc_attr($second), esc_attr($timezone_offset));
}
示例11: LatestTweetsList
public function LatestTweetsList($limit = '5')
{
$conf = SiteConfig::current_site_config();
if (empty($conf->TwitterName) || empty($conf->TwitterConsumerKey) || empty($conf->TwitterConsumerSecret) || empty($conf->TwitterAccessToken) || empty($conf->TwitterAccessTokenSecret)) {
return new ArrayList();
}
$cache = SS_Cache::factory('LatestTweets_cache');
if (!($results = unserialize($cache->load(__FUNCTION__)))) {
$results = new ArrayList();
require_once dirname(__FILE__) . '/tmhOAuth/tmhOAuth.php';
require_once dirname(__FILE__) . '/tmhOAuth/tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array('consumer_key' => $conf->TwitterConsumerKey, 'consumer_secret' => $conf->TwitterConsumerSecret, 'user_token' => $conf->TwitterAccessToken, 'user_secret' => $conf->TwitterAccessTokenSecret, 'curl_ssl_verifypeer' => false));
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array('screen_name' => $conf->TwitterName, 'count' => $limit));
$tweets = $tmhOAuth->response['response'];
$json = new JSONDataFormatter();
if (($arr = $json->convertStringToArray($tweets)) && is_array($arr) && isset($arr[0]['text'])) {
foreach ($arr as $tweet) {
try {
$here = new DateTime(SS_Datetime::now()->getValue());
$there = new DateTime($tweet['created_at']);
$there->setTimezone($here->getTimezone());
$date = $there->Format('Y-m-d H:i:s');
} catch (Exception $e) {
$date = 0;
}
$results->push(new ArrayData(array('Text' => nl2br(tmhUtilities::entify_with_options($tweet, array('target' => '_blank'))), 'Date' => SS_Datetime::create_field('SS_Datetime', $date))));
}
}
$cache->save(serialize($results), __FUNCTION__);
}
return $results;
}
示例12: reverseTransform
/**
* {@inheritdoc}
*/
public function reverseTransform($rfc3339)
{
if (!is_string($rfc3339)) {
throw new TransformationFailedException('Expected a string.');
}
if ('' === $rfc3339) {
return;
}
try {
$dateTime = new \DateTime($rfc3339);
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
if ($this->outputTimezone !== $dateTime->getTimezone()->getName()) {
try {
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
} catch (\Exception $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}
}
if (preg_match('/(\\d{4})-(\\d{2})-(\\d{2})/', $rfc3339, $matches)) {
if (!checkdate($matches[2], $matches[3], $matches[1])) {
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
}
}
return $dateTime;
}
示例13: castFromBuiltin
public static function castFromBuiltin(\DateTime $dt)
{
$newCls = new static();
$newCls->setTimestamp($dt->getTimestamp());
$newCls->setTimezone($dt->getTimezone());
return $newCls;
}
示例14: doFormatObject
/**
* Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
*
* @param \IntlCalendar|\DateTime $object
* @param string|int|array|null $format
* @param string|null $locale
* @return string
* @throws LocalizedException
*/
protected function doFormatObject($object, $format = null, $locale = null)
{
$pattern = $dateFormat = $timeFormat = $calendar = null;
if (is_array($format)) {
list($dateFormat, $timeFormat) = $format;
} elseif (is_numeric($format)) {
$dateFormat = $format;
} elseif (is_string($format) || null == $format) {
$dateFormat = $timeFormat = \IntlDateFormatter::MEDIUM;
$pattern = $format;
} else {
throw new LocalizedException(new Phrase('Format type is invalid'));
}
$timezone = $object->getTimezone();
if ($object instanceof \IntlCalendar) {
$timezone = $timezone->toDateTimeZone();
}
$timezone = $timezone->getName();
if ($timezone === '+00:00') {
$timezone = 'UTC';
} elseif ($timezone[0] === '+' || $timezone[0] === '-') {
// $timezone[0] is first symbol of string
$timezone = 'GMT' . $timezone;
}
return (new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $timezone, $calendar, $pattern))->format($object);
}
示例15: actionCheck
public function actionCheck()
{
$dt = new \DateTime();
echo 'Current date/time: ' . $dt->format('d-m-Y H:i:s');
echo '<br />';
echo 'Current timezone: ' . $dt->getTimezone()->getName();
}