我正在使用一个API函数,它返回一个估计的到达时间,单位是hh:mm left,即。在到达之前还有0:31。
我尝试做的是将返回的hh:mm与当前时间相加,因此最终结果是以UTC为单位的估计到达时间。
我目前有一个非常简单的脚本,它按原样工作,但由于API函数的格式是hh:mm,而strtotime似乎不能识别除整数以外的任何加减,如果在下面的脚本中用+hh:mm替换+07,这将不起作用。
<?php
$time = strtotime("now +07 hours");
print gmdate('H:i T', $time);
?>所以我的最终结果应该是UTC的ETA的hh:mm。
发布于 2011-09-12 00:40:47
一种更灵活的方式:
<?php
function getETA($arrival, $timezone='UTC', $format='H:i T')
{
list($hours,$minutes) = explode(':', $arrival);
$dt = new DateTime('now', new DateTimeZone($timezone));
$di = new DateInterval('PT'.$hours.'H'.$minutes.'M');
$dt->add($di);
return $dt->format($format);
}
?>用法:
<?php
echo getETA('07:10');
echo getETA('07:10', 'America/New_York', 'h:i a T');
?>输出示例:
23:56 UTC
07:56 pm EDT 发布于 2011-09-12 00:21:07
如果您将strtotime参数更改为now +07 hours, +06 minutes,您应该能够添加它们。要将小时和分钟分开,只需使用explode(':', $returnedString)
$returnedString = '07:06';
$returnedTime = explode(':', $returnedString);
$time = strtotime("now +{$returnedTime[0]} hours, +{$returnedTime[1]} minutes");
// Or this
// $time = strtotime('now +' . $returnedTime[0] . ' hours, +' . $returnedTime[1] . ' minutes');
print gmdate('H:i T', $time);发布于 2011-09-12 00:26:56
<?php
$str = "17:26";
$secs = (substr($str, 0, 2) * 3600) + (substr($str, 3, 2) * 60);
echo $secs;
// Output: 62760
?>https://stackoverflow.com/questions/7379543
复制相似问题