2017-08-31 46 views
1

我需要創建一個代碼,它將採用$ time [1]的值並減去$ time [2]的值,然後將$ time [3]的值減去$ time [4]的值,等等,直到耗盡$ time記錄。它應該保持運行總量。選擇記錄php數組對

模擬數據:

$time[1] = "2017-08-28 18:30:00"; 
$time[2] = "2017-08-28 14:00:00"; 
$time[3] = "2017-08-28 13:00:00"; 
$time[4] = "2017-08-28 12:45:00"; 
$time[5] = "2017-08-28 12:30:00"; 
$time[6] = "2017-08-28 12:00:00"; 

例如:[1] - [2] = 4.5小時,[3] - [4] = 0.25小時,[5] - [6] = 0.5小時;總共= 5.25小時。

$hourdiff = round((strtotime($time[1]) - strtotime($time[2]))/3600, 1); //gives you the quantity of hours worked between 2 records 

我需要一種方法來選擇記錄1和2,計算$ hourdiff,將其添加到運行總量,然後選擇記錄3和4,計算$ hourdiff,將它添加到正在運行的總等直到沒有剩下記錄來計算。

回答

1

這聽起來像你尋找for循環:

for ($i = 1; $i <= count($time) - 1; $i++) { 
    $hourdiff = round((strtotime($time[$i + 1]) - strtotime($time[$i]))/3600, 1); 
    echo "The difference is: $hourdiff" . "<br />"; 
} 

這呼應了及時的五個差異。

請注意,您需要從$i開始,因爲您不能從第一個事件中減去「無」!爲了解決這個問題,你需要停止計數的索引。

這可以看出工作here

希望這會有所幫助! :)

+0

Aha非常感謝你爲「for」循環,這是缺失的一部分,我沒有完全理解它應該如何工作(無論它應該是一個while循環或for或foreach循環或其他) 。我剛剛在循環的底部添加了「$ x = $ x + 1」,現在它只計算我需要的成對記錄之間的差異。非常感謝! –

0

迭代通過數組但是,代替將1到迭代變量,添加兩個(並檢查出界失誤):

<?php 
$time[0] = "2017-08-28 18:30:00"; 
$time[1] = "2017-08-28 14:00:00"; 
$time[2] = "2017-08-28 13:00:00"; 
$time[3] = "2017-08-28 12:45:00"; 
$time[4] = "2017-08-28 12:30:00"; 
$time[5] = "2017-08-28 12:00:00"; 
$total = 0; 
for ($i = 0; $i < count($time); $i += 2) { 
    if ($i + 1 > count($time) - 1) break; // check of out of bounds 
    $total += round((strtotime($time[$i]) - strtotime($time[$i + 1]))/3600, 1); // add diff of next two numbers 
} 
echo $total; 

Demo

0

與嘗試:

$time[1] = "2017-08-28 18:30:00"; 
$time[2] = "2017-08-28 14:00:00"; 
$time[3] = "2017-08-28 13:00:00"; 
$time[4] = "2017-08-28 12:45:00"; 
$time[5] = "2017-08-28 12:30:00"; 
$time[6] = "2017-08-28 12:00:00"; 

$times = array(); 

$total = 0; 

foreach ($time as $n => $t0) { 
    if ($n % 2) { 
     continue; 
    } 
    $t1 = strtotime($time[$n - 1]); 
    $t2 = strtotime($t0); 
    $k = sprintf('%s-%s', $n - 1, $n); 
    $diff = ($t1 - $t2)/3600; 
    $times[$k] = sprintf('%.2f hours', $diff); 
    $total += $diff; 
} 


print_r($times); 
printf('TOTAL : %s hours', $total); 

輸出

Array 
(
    [1-2] => 4.50 hours 
    [3-4] => 0.25 hours 
    [5-6] => 0.50 hours 
) 
TOTAL : 5.25 hours 

或者喲可以DateTime::diff

$time[1] = "2017-08-28 18:30:00"; 
$time[2] = "2017-08-28 14:00:00"; 
$time[3] = "2017-08-28 13:00:00"; 
$time[4] = "2017-08-28 12:45:00"; 
$time[5] = "2017-08-28 12:30:00"; 
$time[6] = "2017-08-28 12:00:00"; 

$times = array(); 

$total = new DateTime; 

foreach ($time as $n => $t0) { 
    if ($n % 2) { 
     continue; 
    } 
    $t1 = new DateTime($time[$n - 1]); 
    $t2 = new DateTime($t0); 
    $k = sprintf('%s-%s', $n - 1, $n); 
    $diff = $t1->diff($t2); 
    $times[$k] = $diff->format('%h hour(s) %i min(s) %s sec(s)'); 
    $total = $total->add($diff); 
} 

$total = ($total->diff(new DateTime))->format('%h hour(s) %i min(s) %s sec(s)'); 

print_r($times); 
printf('TOTAL : %s', $total); 

輸出嘗試

Array 
(
    [1-2] => 4 hour(s) 30 min(s) 0 sec(s) 
    [3-4] => 0 hour(s) 15 min(s) 0 sec(s) 
    [5-6] => 0 hour(s) 30 min(s) 0 sec(s) 
) 
TOTAL : 5 hour(s) 15 min(s) 0 sec(s) 
0

使用array_chunk分組爲對,然後array_reduce總結的區別:

$total = array_reduce(
    array_chunk($time, 2), 
    function ($total, $pair) { 
     return $total + (strtotime($pair[0]) - strtotime($pair[1])); 
    }, 
    0 
); 

See it online at 3v4l.org.

輸入$time是時間樣一個字符串數組,適於傳遞給strtotime。由此產生的$total將是一個整數秒。