-1

我正在用这段 php 计算时间差,并将其格式化为天、小时和分钟。

// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);

$interval = $future_date->diff($now);

$enddate = $interval->format("%a days, %h hours, %i minutes");

// if current time is higher than expiration date set contest to finished.
if($now > $future_date) {
    $enddate = 'Date ended';
}

现在我想让格式只显示超过一天(24 小时)的总天数,并在不到一天(24 小时)时开始以小时和分钟为单位进行格式化。所以它会从 23 小时 59 分钟开始格式化为小时和分钟,希望你能明白。

谁能告诉我它是如何做到最简单的?

4

1 回答 1

0

像这样做:

// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);

$interval = $future_date->diff($now);

if ($now > $future_date) {
    // if current time is higher than expiration date set contest to finished.
    $enddate = 'Date ended';
} else if ($interval >= new DateInterval("P1D")) {
    $enddate = $interval->format("%a days, %h hours, %i minutes");
} else {
    $enddate = $interval->format("%a days, %h hours, %i minutes");
}
于 2013-05-20T00:17:12.400 回答