函数名:IntlTimeZone::getOffset()
适用版本:PHP 5 >= 5.5.0, PHP 7, PHP 8
用法:IntlTimeZone::getOffset() 函数用于获取指定时间戳相对于指定时区的偏移量(以秒为单位)。
语法:public static IntlTimeZone::getOffset ( mixed $date [, bool $localtime = false ] ) : int
参数:
- date: 要计算偏移量的时间戳。可以是一个整数时间戳,也可以是一个 DateTime 对象。
- localtime(可选): 指定是否使用本地时间而非 UTC 时间来计算偏移量。默认为 false,即使用 UTC 时间。
返回值:返回一个整数,表示指定时间戳相对于指定时区的偏移量(以秒为单位)。
示例:
// 使用整数时间戳
$timestamp = time();
$timezone = new IntlTimeZone('Asia/Shanghai');
$offset = IntlTimeZone::getOffset($timestamp, false);
echo "相对于亚洲/上海时区的偏移量是:" . $offset . " 秒";
// 使用 DateTime 对象
$date = new DateTime('now', new DateTimeZone('Europe/Paris'));
$timezone = new IntlTimeZone('Asia/Tokyo');
$offset = IntlTimeZone::getOffset($date, true);
echo "相对于亚洲/东京时区的偏移量是:" . $offset . " 秒";
以上示例将根据当前时间获取相对于指定时区的偏移量,并将其以秒为单位输出。第一个示例使用整数时间戳来计算偏移量,第二个示例使用 DateTime 对象并指定了使用本地时间计算偏移量。