时间大于今天时间 php,显示PHP时间大于24小时,如70小时

我有下面的代码显示时间

$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')

我的问题是,如果差异超过24小时,它会在24小时内打破时间并显示1或2或任何小时但低于24小时.

我怎样才能像74小时一样显示真正的小时差异!

谢谢,

解决方法:

理想情况下,我更喜欢以下方法..而不是重新发明轮子或进行大量的手动转换:

$now = new DateTime();

$replydue = new DateTime($listing['replydue_time']);

$timetoreply_hours = $timetoreply->days * 24 + $timetoreply->h;

echo $timetoreply_hours.':'.$timetoreply->format('%I');

days: If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.

请注意,这假设所有日期都是24小时,而在夏令时区可能不是这种情况

我写了以下函数来帮助解决这个问题:

/**

* @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

$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;

}

这可以使用:

$timetoreply = time_diff($replydue, $now);

echo $timetoreply->format('%r%H:%I');

注:由于manual中的注释,我使用了格式(‘U’)而不是getTimestamp().

也不是说post-epoch和pre-negative-epoch日期需要64位!

标签:php,mysql,time

来源: https://codeday.me/bug/20190623/1274374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值