将PHP时间显示为大于24小时(如70小时)的小时
我有下面显示时间的代码将PHP时间显示为大于24小时(如70小时)的小时,php,mysql,time,Php,Mysql,Time,我有下面显示时间的代码 $now = date_create(date("Y-m-d H:i:s")); $replydue = date_create($listing['replydue_time']); $timetoreply = date_diff($replydue, $now); echo $timetoreply->format('%H:%I') Byt我的问题是,如果差异超过24小时,它会在24小时内中断时间,并显示1小时或2小时,或24小时以下的任何时间 我怎样
$now = date_create(date("Y-m-d H:i:s"));
$replydue = date_create($listing['replydue_time']);
$timetoreply = date_diff($replydue, $now);
echo $timetoreply->format('%H:%I')
Byt我的问题是,如果差异超过24小时,它会在24小时内中断时间,并显示1小时或2小时,或24小时以下的任何时间
我怎样才能显示真正的小时差异,比如74小时
谢谢,您可以使用以下代码:
<?php
$date1 = "2014-05-27 01:00:00";
$date2 = "2014-05-28 02:00:00";
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
echo "Difference between two dates is " . $hour = abs($timestamp2 - $timestamp1)/(60*60) . " hour(s)";
?>
试着按照上面的过程。如果你需要帮助,我很乐意帮助你
希望它能起作用
来源:如果您在几小时内喜欢,我将提供这一解决方案:
echo $interval->format('%a')*24+$interval->format('%h');
关于以下注释-也可以采用这种方式:
echo $interval->days*24 + $interval->h;
下面的代码将输出任意两天之间的小时差。在这种情况下,72。希望这有帮助
<?php
$startTime = new \DateTime('now');
$endTime = new \DateTime('+3 day');
$differenceInHours = round((strtotime($startTime->format("Y-m-d H:i:s")) - strtotime($endTime->format("Y-m-d H:i:s")))/3600, 1);
echo $differenceInHours;
理想情况下,我更喜欢以下方法。。而不是重新发明车轮或进行大量手动转换:
$now = new DateTime();
$replydue = new DateTime($listing['replydue_time']);
$timetoreply_hours = $timetoreply->days * 24 + $timetoreply->h;
echo $timetoreply_hours.':'.$timetoreply->format('%I');
从:
天数:如果DateInterval对象是由DateTime::diff()创建的,则这是开始日期和结束日期之间的总天数。否则,日子将是假的
请注意,这假设所有天数均为24小时,但在有DST的地区可能并非如此
我编写了以下函数来帮助实现这一点:
/**
* @param DateTimeInterface $a
* @param DateTimeInterface $b
* @param bool $absolute Should the interval be forced to be positive?
* @param string $cap The greatest time unit to allow
*
* @return DateInterval The difference as a time only interval
*/
function time_diff(DateTimeInterface $a, DateTimeInterface $b, $absolute=false, $cap='H'){
// Get unix timestamps
$b_raw = intval($b->format("U"));
$a_raw = intval($a->format("U"));
// Initial Interval properties
$h = 0;
$m = 0;
$invert = 0;
// Is interval negative?
if(!$absolute && $b_raw<$a_raw){
$invert = 1;
}
// Working diff, reduced as larger time units are calculated
$working = abs($b_raw-$a_raw);
// If capped at hours, calc and remove hours, cap at minutes
if($cap == 'H') {
$h = intval($working/3600);
$working -= $h * 3600;
$cap = 'M';
}
// If capped at minutes, calc and remove minutes
if($cap == 'M') {
$m = intval($working/60);
$working -= $m * 60;
}
// Seconds remain
$s = $working;
// Build interval and invert if necessary
$interval = new DateInterval('PT'.$h.'H'.$m.'M'.$s.'S');
$interval->invert=$invert;
return $interval;
}
N.B.我使用了format('U')
而不是getTimestamp()
,因为
而且,纪元后和纪元负数前的日期也不需要64位 您必须将两者转换为秒,并以秒为单位获得差值。然后你应该把秒和小时的差值转换成小时。@WilliamFrancisGomes-你应该但不应该有。Thnx@KanchoIliev,我的坏:)可能是重复的,我本来打算投这个。。但是DateInterval类中有一些属性允许您在不使用format函数的情况下访问这些数字。这是事实,但通过这种方式显示,我认为想法非常清楚。更清楚的是,您使用两个字符的字符串来提示从间隔转换为字符串。然后隐式地将它们转换为数字,进行计算并将其更改回字符串。。你真的认为格式('%a')
比days
?谢谢:)更清晰,但我使用'a'时有一些逻辑,但对我糟糕的英语来说可能很难。我喜欢用“a”这个词来表达这样一个想法,即它不是直接与天联系在一起的。也有d和d,但它们类似于一个h,最多返回31天(类似于一个h,最多返回24天)。这就是我用“a”的方式。如果我使用了->天,如果限制为31天,直接关联将不会清除该问题。这是我的逻辑-可能有点奇怪,但。。。总之,你是对的,说得对。。我同意这有点令人困惑。不过,我宁愿用注释来澄清这一点,也不愿使用带有模糊参数的循环方法!你确定这是正确的吗$timetoreply->days*24,因为如果我使用这个,我可以得到奇怪的小时数,如果我使用$interval->format(“%a”)*24,我可以得到正确的小时数。嗯,太好了!现在就拿到了,并且转换了!这是否意味着将两者都转换为UNIX时间,然后进行减法运算,正如威廉·弗朗西斯在下面的帖子中给出的那样?@rjcode确实如此。。但是如果可能的话,我喜欢使用PHP对象。
$timetoreply = time_diff($replydue, $now);
echo $timetoreply->format('%r%H:%I');