[英]PHP add time duration in while loop
我正在通過登錄時間和注銷時間的差異計算 while 循環中的持續時間。 我想在變量中添加所有持續時間並將其打印出來。 我正在使用的代碼是 -
$totaltimespent = new DateTime;
$totaltimespent->setTime(0, 0);
$timespent= (strtotime($totaltimespent->format("H:i:s")));
while ($row = mysqli_fetch_array($result)) {
echo $row['timeoflogin'];
echo $row['logouttime'];
$startTime = new DateTime($row['timeoflogin']);
$endTime = new DateTime($row['logouttime']);
$duration = $startTime->diff($endTime);
echo $duration->format("%H:%I:%S");
$converttime= (strtotime($duration->format("%H:%I:%S")));
$timespent = date("H:i:s",$converttime+$timespent);
}
echo $timespent;
登錄時間和注銷時間的格式為 - 05:03:53pm。 $duration 給出了正確的結果。 我想在變量中添加所有持續時間並在 while 循環后打印。 請幫幫我。
您可以簡單地將您已經在循環中計算出的差值添加到另一個DateTime
對象中,並獲得它們之間的最終差值。
$totalStart = new DateTime('today'); // this will create it with time 00:00:00
$totalEnd = new DateTime('today'); // we will use this to add the intervals from the loop
while ($row = mysqli_fetch_array($result)) {
...
$startTime = new DateTime($row['timeoflogin']);
$endTime = new DateTime($row['logouttime']);
$duration = $startTime->diff($endTime);
$totalEnd->add($duration);
...
}
$totalTimeSpent = $totalStart->diff($totalEnd);
現在您需要做的就是按照您想要的方式對其進行格式化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.