2014-02-14 77 views
0

我需要一些幫助來計算當前分鐘的當前分鐘:秒在足球比賽中出場,讓我們假設一個足球比賽有:PHP - 計算的足球比賽

$schedule='2014-02-13'; 
$start_time1='03:00 pm'; 
$end_time1='03:45 pm'; 
$start_time2='04:00 pm'; 
$end_time2='04:50 pm'; 
$start_extra_time1='05:10 pm'; 
$end_extra_time1='05:30 pm'; 
$start_extra_time2='05:35 pm'; 
$end_extra_time2='06:10 pm'; 

我其實我現在的時間是「05:32 pm」,我如何計算當前比賽的分鐘數:秒或(小時:分鐘:秒)?

我已經嘗試了許多解決方案,比如datediff,strtotime等......但是沒有運氣,因爲這些日期之間有一段關閉時間。

非常感謝您的幫助。

+3

請勿使用字符串來表示日期或時間。使用['datetime'](http://nl3.php.net/datetime)對象。 – Carpetsmoker

回答

-1

Mysql已經有timeDiff測量一個bug,所以用這個:

Time_To_Sec($currentime) - Time_To_Sec($end_time2) As time_seconds 

還要檢查你的時間格式,避免使用字符串作爲時間或日期,格式,日期/時間的實體。

+1

這與MySQL有什麼關係? –

+0

你是對的,沒有正確閱讀。 OP詢問PHP, – datelligence

0

下面介紹如何使用DateTime()DateInterval()做到這一點:

<?php 
$schedule='2014-02-13'; 
$start_time1='03:00 pm'; 
$end_time1='03:45 pm'; 
$start_time2='04:00 pm'; 
$end_time2='04:50 pm'; 
$start_extra_time1='05:10 pm'; 
$end_extra_time1='05:30 pm'; 
$start_extra_time2='05:35 pm'; 
$end_extra_time2='06:10 pm'; 

$start1 = new DateTime($start_time1); 
$end1 = new DateTime($end_time1); 
$firstInterval = $start1->diff($end1); 

$start2 = new DateTime($start_time2); 
$end2 = new DateTime($end_time2); 
$secondInterval = $start2->diff($end2); 

$start3 = new DateTime($start_extra_time1); 
$end3 = new DateTime($end_extra_time1); 
$thirdInterval = $start3->diff($end3); 

$start4 = new DateTime($start_extra_time2); 
$end4 = new DateTime($end_extra_time2); 
$fourthInterval = $start4->diff($end4); 

$baseline = new DateTime(); 
$accrual = clone $baseline; 
$accrual->add($firstInterval); 
$accrual->add($secondInterval); 
$accrual->add($thirdInterval); 
$accrual->add($fourthInterval); 
$difference = $accrual->diff($baseline); 
echo $difference->format('%h hours, %i minutes'); 

See it in action

所有這些代碼所做的是:

  1. 創建代表各的開始和結束時間DateTime()對象時間間隔。

  2. 得到一個DateInterval()對象代表兩者之間的差異。

  3. 創建基線(起始點)DateTime()對象

  4. 創建一個DateTime()對象,我們增加我們的間隔代表時間流逝

  5. 回聲出兩個

之間的區別

如果您使用PHP 5.4或更新版本,您可以縮短其中的一部分:

$firstInterval = (new DateTime($start_time1))->diff((new DateTime($end_time1))); 
$secondInterval = (new DateTime($start_time2))->diff((new DateTime($end_time2))); 
$thirdInterval = (new DateTime($start_extra_time1))->diff((new DateTime($end_extra_time1))); 
$fourthInterval = (new DateTime($start_extra_time2))->diff((new DateTime($end_extra_time2))); 

$accrual->add($firstInterval)->add($secondInterval)->add($thirdInterval)->add($fourthInterval); 
+0

不僅僅是完美的約翰!謝謝。我有幾個問題: 1-可能也顯示秒? 2-可以手動設置我的時間嗎?例如如果足球比賽是UTC,並且我有UTC + 5,那麼是否存在時區差異的解決方案? – Zeta

+0

要添加秒,只需使用'%s秒'。要將時區添加到混音中,可以使用['DateTimeZone()'](http://us2.php.net/manual/en/class.datetimezone.php)。 Google快速搜索會向您顯示如何使用它。 –

+0

好的約翰,我如何手動添加我的約會?而不是$ baseline = new DateTime();因爲在將其轉換爲UTC後,可以通過參數傳遞此日期。 – Zeta

0

我加入了約翰的解決方案,這是我的代碼:

<?php 

date_default_timezone_set("Canada/Eastern"); 
$date="2014-02-14 14:00:00 +00"; 


$start_time1='09:00 am'; 
$end_time1='09:45 am'; 
$start_time2='10:00 am'; 
$end_time2='10:50 am'; 
$start_extra_time1=''; 
$end_extra_time1='';  
$start_extra_time2=''; 
$end_extra_time2='';   

$start1 = new DateTime($start_time1); 
$end1 = new DateTime($end_time1); 
$firstInterval = $start1->diff($end1); 

$start2 = new DateTime($start_time2); 
$end2 = new DateTime($end_time2); 
$secondInterval = $start2->diff($end2); 

$start3 = new DateTime($start_extra_time1); 
$end3 = new DateTime($end_extra_time1); 
$thirdInterval = $start3->diff($end3); 

$start4 = new DateTime($start_extra_time2); 
$end4 = new DateTime($end_extra_time2); 
$fourthInterval = $start4->diff($end4); 

$baseline = new DateTime($date); 
$baseline->setTimezone(new DateTimeZone('Canada/Eastern')); // -05 

$accrual = new DateTime(); 

$accrual->add($firstInterval); 
$accrual->add($secondInterval); 
$accrual->add($thirdInterval); 
$accrual->add($fourthInterval); 
$difference = $accrual->diff($baseline); 

echo "baseline time :\n"; 
var_dump($baseline); 
echo "accrual time :\n"; 
var_dump($accrual); 

print json_encode(array('response'=>$difference->format('%h hours, %i minutes, %s seconds'))); 

?> 

比賽尚未開始,但它顯示出1個小時的比賽過去了,它是如何可能? http://easycaptures.com/fs/uploaded/645/4891434894.png