略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DateTime::getOffset

2024-12-21

DateTime::getOffset

DateTimeImmutable::getOffset

DateTimeInterface::getOffset

date_offset_get

(PHP 5 >= 5.2.1, PHP 7, PHP 8)

DateTime::getOffset -- DateTimeImmutable::getOffset -- DateTimeInterface::getOffset -- date_offset_getReturns the timezone offset

说明

面向对象风格

public DateTime::getOffset(): int
public DateTimeImmutable::getOffset(): int
public DateTimeInterface::getOffset(): int

过程化风格

Returns the timezone offset.

参数

object

仅为过程化风格:由 date_create() 返回的 DateTime 类型的对象。

返回值

Returns the timezone offset in seconds from UTC on success.

更新日志

版本 说明
8.0.0 Prior to this version, false was returned on failure.

范例

示例 #1 DateTime::getOffset() example

面向对象风格

<?php
$winter 
= new DateTime('2010-12-21', new DateTimeZone('America/New_York'));
$summer = new DateTime('2008-06-21', new DateTimeZone('America/New_York'));

echo 
$winter->getOffset() . "\n";
echo 
$summer->getOffset() . "\n";
?>

过程化风格

<?php
$winter 
date_create('2010-12-21'timezone_open('America/New_York'));
$summer date_create('2008-06-21'timezone_open('America/New_York'));

echo 
date_offset_get($winter) . "\n";
echo 
date_offset_get($summer) . "\n";
?>

以上例程会输出:

-18000
-14400

Note: -18000 = -5 hours, -14400 = -4 hours.

add a noteadd a note

User Contributed Notes 1 note

up
2
s7sunder at gmail dot com
8 years ago
This will be useful for converting offset values into GMT format

<?php

 
//target time zone
 
$target_time_zone = new DateTimeZone('America/Los_Angeles');

 
//find kolkata time
 
$kolkata_date_time = new DateTime('now', $target_time_zone);
 
 
//get the exact GMT format
 
echo 'GMT '.$kolkata_date_time->format('P');

官方地址:https://www.php.net/manual/en/datetime.getoffset.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3