-1
我发现了一段时间的方式,但我忘了它是如何做到的。有谁知道如何使用php
函数DateTime()
将1:00am
转换为25:00
?如何在DateTime中将凌晨1点到25点的军事时间转换为
我发现了一段时间的方式,但我忘了它是如何做到的。有谁知道如何使用php
函数DateTime()
将1:00am
转换为25:00
?如何在DateTime中将凌晨1点到25点的军事时间转换为
我想帮助你:
function timeLength($sec)
{
$s=$sec % 60;
$m=(($sec-$s)/60) % 60;
$h=floor($sec/3600);
return $h.":".substr("0".$m,-2).":".substr("0".$s,-2);
}
echo timeLength(6534293); //outputs "1815:04:53"
如果你真的想用DateTime对象,这里有一个(一种作弊的)解决方案:
function dtLength($sec)
{
$t=new DateTime("@".$sec);
$r=new DateTime("@0");
$i=$t->diff($r);
$h=intval($i->format("%a"))*24+intval($i->format("%H"));
return $h.":".$i->format("%I:%S");
}
echo dtLength(6534293); //outputs "1815:04:53" too
如果你需要它OO并没有介意创建自己的类,你可以尝试
class DTInterval
{
private $sec=0;
function __construct($s){$this->sec=$sec;}
function formet($format)
{
/*$h=...,$m=...,$s=...*/
$rst=str_replace("H",$h,$format);/*etc.etc.*/
return $rst;
}
}
我不知道任何时间使用'25:00'的系统。这是一个错字吗?军事是'00:00-23:59'(或我认为?)。 – chris85
它不是一个错字。我看到它在PHP中显示凌晨1点25 ... – json2021
你从哪里看到的?你能发布代码吗? – chris85