2012-05-22 121 views
0

未來時間:2012-05-26 00:00:00如何獲得小時和分鐘?

應該有三個變量:$hour $minute $second

現在,我想用未來的時間減去現在的時間。然後給左邊的小時$hour,給左邊的分鐘$minute,給左邊的第二個$second

我很抱歉,我是新的PHP,現在我陷入困境如何做數學運算?謝謝

日期和時間
+1

閱讀[手冊](http://www.php.net/manual/en/datetime.diff.php)或[使用搜索功能](http://stackoverflow.com/questions/) 365191/how-to-get-time-in-minutes-in-php) – PenguinCoder

回答

0

以下是小時,分鐘的差異的數學運算和秒

$future_datetime = '2012-05-26 00:00:00'; 
$future = strtotime($future_datetime); //future datetime in seconds 
$now_datetime = date('Y-m-d H:i:s'); 
$now = date('U'); //now datetime in seconds 

//The math for calculating the difference in hours, minutes and seconds 
$difference = $future - $now; 
$second = 1; 
$minute = 60 * $second; 
$hour = 60 * $minute; 
$difference_hours = floor($difference/$hour); 
$remainder = $difference - ($difference_hours * $hour); 
$difference_minutes = floor($remainder/$minute); 
$remainder = $remainder - ($difference_minutes * $minute); 
$difference_seconds = $remainder; 
echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds"; 
+0

非常感謝,爲什麼要加$ second = 1;謝謝 – run

+0

只是爲了讓你更容易理解這個概念:) 你可以用'$ minute = 60' – ephemeron

1

檢查date_diff功能。這裏有你要問的問題的確切解決方案。

here's the page (DateInterval::format)記錄如何格式化輸出。

$now = date_create(); 
// use "now" and necessary DateTimeZone in the arguments 
$otherDate = date_create('2020-04-13'); 
$interval = date_diff($now, $futureDate); 
echo $interval->format('%a days');