iso8601_timezone_to_offset()
云策文档标注
概述
iso8601_timezone_to_offset() 函数用于将 ISO 8601 时区字符串转换为 UTC 偏移秒数。它支持 'Z' 表示零偏移或 '±hhmm' 格式的时区字符串。
关键要点
- 参数 $timezone 为必需,可以是 'Z' 或 '±hhmm' 格式的字符串。
- 返回值为整数或浮点数,表示 UTC 偏移秒数。
- 函数内部处理逻辑:若为 'Z' 则偏移为 0;否则解析符号、小时和分钟计算偏移。
代码示例
function iso8601_timezone_to_offset( $timezone ) {
// $timezone is either 'Z' or '[+|-]hhmm'.
if ( 'Z' === $timezone ) {
$offset = 0;
} else {
$sign = ( str_starts_with( $timezone, '+' ) ) ? 1 : -1;
$hours = (int) substr( $timezone, 1, 2 );
$minutes = (int) substr( $timezone, 3, 4 ) / 60;
$offset = $sign * HOUR_IN_SECONDS * ( $hours + $minutes );
}
return $offset;
}
原文内容
Given an ISO 8601 timezone, returns its UTC offset in seconds.
Parameters
$timezonestringrequired-
Either
'Z'for 0 offset or'±hhmm'.
Source
function iso8601_timezone_to_offset( $timezone ) {
// $timezone is either 'Z' or '[+|-]hhmm'.
if ( 'Z' === $timezone ) {
$offset = 0;
} else {
$sign = ( str_starts_with( $timezone, '+' ) ) ? 1 : -1;
$hours = (int) substr( $timezone, 1, 2 );
$minutes = (int) substr( $timezone, 3, 4 ) / 60;
$offset = $sign * HOUR_IN_SECONDS * ( $hours + $minutes );
}
return $offset;
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |