我正在计算这个时间差异,并在几天,几小时和几分钟内格式化它。
// 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分钟开始,它将格式化为小时和分钟,您希望得到这个想法。
有谁能告诉我它是如何做到最简单的?
答案 0 :(得分: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");
}