2011-01-06 220 views
2

假設目標時間爲下午4點30分,當前時間爲下午3點25分,我如何計算剩餘時間以達到目標時間?我需要在幾分鐘內的結果。PHP:如何計算剩餘時間以分鐘爲單位?

session_start(); 
$m=30; 
//unset($_SESSION['starttime']); 
if(!$_SESSION['starttime']){ 
    $_SESSION['starttime']=date('Y-m-d h:i:s'); 
} 
$stime=strtotime($_SESSION['starttime']); 
$ttime=strtotime((date('Y-m-d h:i:s',strtotime("+$m minutes"))));-->Here I want to calcuate the target time; the time is session + 30 minutes. How will i do that 
echo round(abs($ttime-$stime)/60); 

Krishnik

+1

可能的重複[如何獲得時間差在PHP分鐘](http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – miku 2011-01-06 10:55:03

+0

strtotime (「+30分鐘」);返回當前時間+ 30分鐘。我怎樣才能將時間以外的時間傳遞給strtotime(); ? – Nick 2011-01-06 11:38:17

回答

3

兩個時間之間的差異的快速計算可以這樣做:

$start = strtotime("4:30"); 
    $stop = strtotime("6:30"); 
    $diff = ($stop - $start); //Diff in seconds 
    echo $diff/3600; //Return 2 hours. Divide by something else to get in mins etc. 

編輯* 還不如答案添加到您的問題太多:

$start = strtotime("3:25"); 
$stop = strtotime("4:30"); 
$diff = ($stop - $start); 
echo $diff/60; //Echoes 65 min 

哦,還有一個編輯:)如果時間是不同的日期,就像開始時間是23:45,結束時間是0:30,接下來你需要爲時間戳添加一個日期。