Php 如何获取当前午夜时间内的UTC偏移量?
我想在任何给定时刻确定哪个UTC偏移量介于Php 如何获取当前午夜时间内的UTC偏移量?,php,date,datetime,utc,Php,Date,Datetime,Utc,我想在任何给定时刻确定哪个UTC偏移量介于00:00到00:59之间 有没有一种简洁的方法可以在不需要手动迭代偏移量的情况下实现这一点?也许通过UTC当前时间的转换?使用DateTime和DateTimeZone1,您可以创建一个有用的函数: /* Return UTC Offsets/Timezones in which is 00AM at passed time string @param string original time string (defaul
00:00
到00:59
之间
有没有一种简洁的方法可以在不需要手动迭代偏移量的情况下实现这一点?也许通过UTC当前时间的转换?使用
DateTime
和DateTimeZone
1,您可以创建一个有用的函数:
/*
Return UTC Offsets/Timezones in which is 00AM at passed time string
@param string original time string (default: current time)
@param string original timezone string (default: UTC)
@param bool return as Timezones instead as UTC Offests (default: False)
@retval array array of UTC Offsets or Timezones
*/
function getMidNight( $timeString=Null, $timeZone=Null, $returnTimeZone=False )
{
$utc = new DateTimeZone( 'UTC' );
$baseTimeZone = ( $timeZone ) ? new DateTimeZone( $timeZone ) : $utc;
$date = new DateTime( $timeString, $baseTimeZone );
$retval = array();
foreach( DateTimeZone::listIdentifiers() as $tz )
{
$currentTimeZone = new DateTimeZone( $tz );
if( ! $date->setTimezone( $currentTimeZone )->format('G') )
{
if( $returnTimeZone ) $retval[] = $tz;
else $retval[] = $date->getOffset();
}
}
return array_unique( $retval );
}
G
格式是24小时不前导零,因此at00
是False
。
返回所有已定义时区标识符的列表
然后,这样称呼它2:
您将获得3:
这样称呼它2:
您将获得:
Array
(
[0] => Antarctica/McMurdo
[1] => Pacific/Auckland
[2] => Pacific/Enderbury
[3] => Pacific/Fakaofo
[4] => Pacific/Midway
[5] => Pacific/Niue
[6] => Pacific/Pago_Pago
[7] => Pacific/Tongatapu
)
4
笔记:
这里的情况如何?我有一个问题,我正在寻找解决,我没有一个现有的算法。看起来很合适。关键是你必须试一试,否则我们不知道你有什么问题。如果有人在这里实现了什么,那只是解决了你的问题。如果你展示了你已经尝试过的,那么这个问题更有可能对其他人有用。
Array
(
[0] => 46800
[1] => -39600
)
print_r( getMidNight( Null, Null, True ) );
Array
(
[0] => Antarctica/McMurdo
[1] => Pacific/Auckland
[2] => Pacific/Enderbury
[3] => Pacific/Fakaofo
[4] => Pacific/Midway
[5] => Pacific/Niue
[6] => Pacific/Pago_Pago
[7] => Pacific/Tongatapu
)