2013-05-18 36 views
-1

我正在計算這個php的時間差,並以天,小時和分鐘格式化它。根據天格式化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分鐘開始的幾小時和幾分鐘,希望您有所想法。

誰能告訴我它是如何做到最簡單的?

+1

如果檢查的時間和格式accordinly –

+0

長度/ else語句這很簡單,如果你想想看一點,你會拿出自己的答案。 – vascowhite

回答

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"); 
}