2017-09-23 39 views
0

我有一個像下面兩個時間值:如何在php中查找兩個時間值的中間值?

$ A = 22:15

$ B = 28:50

28:50將意味着上午4點50

我如何找出這兩個時間值的中間值?在簡單的數學,這將是象下面這樣:

$ C =($ $ B- A)/ 2

*(下面線編) 回波$ C + $一個;

當然這不是那麼簡單,我做了谷歌和到處搜尋,但徒勞無功。

由於一噸提前!

+0

難道這些字符串或時間戳? –

+0

將它們轉換爲分鐘或秒,執行簡單的數學平均值,並轉換回你所需要的格式。 – Calimero

+0

你解決了問題嗎? – VK321

回答

1

我會強烈建議你用timestamps工作。 然後,你可以簡單地做到以下幾點:

// Assuming $b and $a as timestamps 
$c = ($b-$a)/2; 
echo date('H:i', $c); 
+0

所以,你的意思是,我的日期值增加時,將其轉換成UNIX的時間戳,然後做計算的,對不對?我會嘗試這個伴侶,謝謝! – Beginner

+0

這將是最好的忽略時間的字符串,並使用時間戳,而不是隻要你能。這可能會幫助你:[php日期時間參考](http://php.net/manual/en/ref.datetime.php)。 –

0

我一點都沒有在過去5年中任何代碼,但是,我很高興,我能與你們的幫助下實現代碼..在這裏下面的代碼我一直在尋找:

<? 
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 
?> 

<form action="" method="post"> 
Date-Time 1: <input type="text" name="time1"><br> 
Date-Time 2: <input type="text" name="time2"><br> 
<input type="submit"> 
</form> 


<? 

if (!empty($_POST)) 
{ 

$time1 = $_POST["time1"]; 
$time2 = $_POST["time2"]; 

// echo $time1 . "<br>"; 

$utime1 = strtotime($time1); 
$utime2 = strtotime($time2); 

$temp = $utime2 - $utime1; 
$temp = $temp/2; 

$MidTime = $utime1 + $temp; 


echo "Time 1: " . $time1 . "<br>"; 
echo "Time 2: " . $time2 . "<br><br>"; 

echo "U-Time 1: " . $utime1 . "<br>"; 
echo "U-Time 2: " . $utime2 . "<br><br>"; 

echo "UTime-2 minus UTime-1 in Unix: " . $temp . "<br><br>"; 


echo "Mid Time: " . gmdate("Y-m-d H:i:s ", $MidTime); 

} 


?> 

enter image description here

相關問題