2016-04-29 106 views
0

對於我的生活,我無法在互聯網上找到有關如何解決我的問題的任何文檔的任何地方。我已經看到了大量計算時間差異的方法,但我似乎無法將一些適合我情況的東西放在一起。PHP - 多時差計算

我目前正在構建一個時間管理/門票系統,它是一個WordPress插件。我使用自定義表格而不是WordPress表格。我有一個包含所有主要票據信息的表格和另一個包含所有開始/停止時間的表格。一旦用戶將票標記爲完成,它將獲取所有開始/停止時間值並計算所花費的總時間,然後將該總時間值放入主票單表中的「total_time」列中。

我查詢數據庫和拉動起動/使用停止時間:
$start_times = $wpdb->get_results($start_times_query); $stop_times = $wpdb->get_results($stop_times_query);

現在我有2陣列的信息。這是我應該這樣做的方式嗎?

陣列的給我這個(爲開始時間): Array ([0] => Array ([time] => 2016-04-29 12:02:43) [1] => Array ([time] => 2016-04-29 12:04:18) [2] => Array ([time] => 2016-04-29 12:06:07) [3] => Array ([time] => 2016-04-29 12:07:56) [4] => Array ([time] => 2016-04-29 12:10:30) [5] => Array ([time] => 2016-04-29 12:11:59))

(爲結束時間的格式相同)

然後我打破使用數組:現在

$startTimes = array_column($startTimes, 'time'); 
$endTimes = array_column($endTimes, 'time'); 

給我這個陣列(這個只是開始時間,但結束時間的格式相同):

Array ([0] => 2016-04-29 12:02:43 [1] => 2016-04-29 12:04:18 [2] => 2016-04-29 12:06:07 [3] => 2016-04-29 12:07:56 [4] => 2016-04-29 12:10:30 [5] => 2016-04-29 12:11:59) 

通常我可以通過一個foreach ($start_time as $time){}來遍歷所有的值,但是(糾正我,如果我錯了)我不能把兩個數組放到foreach括號中,我不能把另一個foreach語句放在當前的語句中因爲那麼它將只返回第二個數組的所有時間,並且只返回第一個數組的一個值。我覺得我錯過了一些簡單的事情,我該怎麼做才能做到這一點?

編輯
感謝Scopey我已經摸索出了while循環,但它無法正常工作。以下是我有:

$startTimes = array_column($startTimes, 'time'); 
$endTimes = array_column($endTimes, 'time'); 
while (($startTime = current($startTimes)) !== false && ($endTime = current($endTimes) !== false)) { 

    $startTimesConv = strtotime($startTimes); 
    $endTimesConv = strtotime($endTimes); 
    $totalTime = ($startTimesConv - $endTimesConv)/60/60; 

    next($startTimes); 
    next($endTimes); 
} 
+0

任何人都可以幫我理清while循環嗎? – Mason

回答

0

如果要循環兩個數組在同一時間,你可以使用PHP函數通過操縱其內部指針遍歷數組:currentnextresetendprev

你只需要做出一個while循環:

while (($startTime = current($startTimes)) !== false && ($endTime = current($endTimes) !== false) { 
    // ... Do things 

    next($startTimes); 
    next($endTimes); 
} 

查看文檔nextcurrent

+0

如何在while循環中對這樣的數組進行計算: Start Times = 'Array([0] => Array([time] => 2016-04-29 12:02:43) [1] => Array([time] => 2016-04-29 12:04:18)[2] => Array([time] => 2016-04-29 12:06:07)[3] = > Array([time] => 2016-04-29 12:07:56)[4] => Array([time] => 2016-04-29 12:10:30)[5] => Array([時間] => 2016-04-29 12:11:59))' (相同格式的結束時間) – Mason

+0

您可以使用['array_column'](http://php.net/manual/en/function .array-column.php)將數組獲取到一個維度:'$ startTimes = array_column($ startTimes,'time');' - 然後使用我展示的while循環。 – Scopey

+0

好吧我近...這是我的while循環的樣子,但它不起作用。你能指出什麼是錯的嗎? '而(($ STARTTIME =電流($ startTimes))!= =假&&($結束時間=電流($ endTimes)!== FALSE){ \t \t \t \t \t \t \t \t \t \t \t \t $ startTimesConv =的strtotime($ startTimes); \t \t \t \t \t \t $ endTimesConv =的strtotime($ endTimes); \t \t \t \t \t \t $ totalTime =($ startTimesConv - $ endTimesConv)/ 60/60; \t \t \t \t \t \t \t \t \t \t \t \t下一個($ startTimes); \t \t \t \t \t \t next($ endTimes); \t \t \t \t \t} ' – Mason

0

我會退後一步,看看查詢本身。您試圖在數據庫中兩次相關的計算之間進行計算,但通過將時間分隔爲兩個單獨的數組,會損失關係。或者至少與數組索引不明確相關。

我的僞代碼是這樣的:

#For each ticket which is closed 
    #Get an array (time entries) of arrays (start, stop) (each having it's start and stop time) 
    #total_time = 0 
    #For each time entry 
     #Perform a time calc function to get_time_differnce(start, stop) 
     #Add the time to Total_time 
    #Add total_time to ticket record in DB 

如果你表現出一些細節,我也許可以擴大。 (即查詢本身)

0

要遍歷兩個經常數組長度相同使用for代替foreach

$length = count($start_times); 
for ($i = 0; $i < $length; $i++) { 
    $start_time = $start_times[$i]["time"]; 
    $stop_time = $stop_times[$i]["time"]; 
    // do things 
} 

但在你的情況,我強烈建議計算SQL的差異。

+0

我不是太熟悉SQL,我知道的基本知識,知道足以讓周圍。你會如何推薦使用SQL路線來進行計算? – Mason

+0

加入您的查詢,並使用DATEDIFF。請參閱https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff –