2012-03-24 50 views
-5

可能重複:
How to calculate the difference between two dates using PHP?差之間的兩個日期和在輸入顯示它通過使用PHP

我在HTML三個輸入。其中兩個是開始日期和結束日期,第三個是找出它們之間的差異並顯示結果。

這裏是輸入:

<p><span>Start date:</span> <input type="date" required min="2012-01-01" name="start_d" id="start_d" ><span>* Format: YYYY-MM-DD</span><br></p> 
<p><span>End date:</span> <input type="date" required min="2012-01-01" name="end_d" id="end_d" ><span>* Format: YYYY-MM-DD</span><br></p> 
<p><span>Duration :</span><input type="text" required name="duration" id=duration" readonly="readonly"><br></p> 
+1

嘗試'的strtotime()'函數 – safarov 2012-03-24 20:48:48

+0

一請就爲了看一看。 – yehuda 2012-03-24 20:49:17

回答

0
date('Y-m-d', (strtotime($str_start_date) + round((strtotime($str_end_date) - strtotime($str_start_date))/2))'; 

編輯:

Missunderstood你的問題。雖然你想要這些之間的日期?你想在幾秒鐘內的差異?

round((strtotime($str_end_date) - strtotime($str_start_date))/2) 
+0

我應該在哪裏使用它?我的意思是我想在第三次輸入中查看結果,我該怎麼做? – user1290487 2012-03-24 20:55:12

+0

您希望在選擇其他2個值時生成此項?這將是一個JavaScript解決方案,而不是標記它的PHP。 – 2012-03-24 21:00:57

+0

所以它將與JavaScript一起使用。怎麼樣? – user1290487 2012-03-24 21:06:49

1
$date1 = new DateTime("2011-02-01"); 
$date2 = new DateTime("2012-01-01"); 
$duration = $date1->diff($date2); 
var_dump($duration); 
相關問題