是否存在从PHP中的DateTime对象提取总小时数的快捷方法?


Does there exist a shorthand method for extracting the total hour count from a DateTime object in PHP?

给定-

$Foo = new DateTime();

是否有可能从这个值中提取总小时数(非分数),除了像…

$Bar = (($Foo->format('Y') * 365) + $Foo->format('d')) * 24

我希望是这样的

echo $Foo->tHours(); /*Would output the total number of hours since the UNIX clock started.*/

这样的东西存在吗,或者我只是像我把值赋给上面的$Bar的方式一样被困在洞穴里?

DateTime有一个getTimestamp方法,它为您提供了一个UNIX时间戳(从epoch经过的秒数)。因此,您可以使用它来更准确地计算小时数(只需除以60*60)。

$Bar = floor($Foo->getTimestamp() / (60 * 60));
echo "Hours since $Foo->format('c') = $Bar"; // Hours since 2015-10-12T18:42:39-04:00 = 401302