如何在PHP中添加日期数组?

时间:2015-05-19 07:16:27

标签: php arrays codeigniter

美好的一天,我如何使用此代码示例添加日期数组: 所有这些数据都来自我的数据库查询,我正在使用codeigniter。

我在我的模型中有这个查询:

[18/May/2015:16:35:11 +0200] "\xac\xc4>\x90X\x7f\xdb\x18\x85\xf7\xcb\xd8FN\x1b\x1e\\\x89i\x13\xdf A\xae\x80\xa3\xee\xbaA\xa3\x1f\xc9orQi\xbd?\x1eD\x94\x87\xe7\x94HA\x83\xa0\x8b\xad\x14\x13tl|2M<\xdf\xf8\xe9\xb7\x04\xe5\xb0\x85\xd1\xf8\x07\xd9~\xca#-\t\x8e\xd3\x13!I\x82;\xe0\x97zX\x01\xc4\x03\xda\xcb10\x8b\x9b\xd0\xe1\xb2\xd6\xd7\x02\xfd\x01\xc5D\xc6\x06" 400 226
[18/May/2015:16:35:11 +0200] "POST /cgi-bin/upload.pl HTTP/1.1" 200 -
[18/May/2015:16:35:15 +0200] "GET /cgi-bin/md5.pl HTTP/1.1" 500 528
[18/May/2015:16:38:21 +0200] "\xac\xc4>\x90X\x7f\xdb\x18\x85\xf7\xcb\xd8FN\x1b\x1e\\\x89i\x13\xdf A\xae\x80\xa3\xee\xbaA\xa3\x1f\xc9orQi\xbd?\x1eD\x94\x87\xe7\x94HA\x83\xa0\x8b\xad\x14\x13tl|2M<\xdf\xf8\xe9\xb7\x04\xe5\xb0\x85\xd1\xf8\x07\xd9~\xca#-\t\x8e\xd3\x13!I\x82;\xe0\x97zX\x01\xc4\x03\xda\xcb10\x8b\x9b\xd0\xe1\xb2\xd6\xd7\x02\xfd\x01\xc5D\xc6\x06" 400 226
[18/May/2015:16:38:21 +0200] "POST /cgi-bin/upload.pl HTTP/1.1" 200 24

这个foreach在我看来。     

SELECT 
  tbl_fdtr.id,
  tbl_fdtr.emp_id,
  date(tbl_fdtr.in_out) AS dtr_date,
  SUM(time(tbl_fdtr.in_out)) AS dtr_time,
  subtime(max(time(tbl_fdtr.in_out)), min(time(tbl_fdtr.in_out))) AS timediffs,
  tbl_fdtr.status,
  tbl_employee.emp_id,
  tbl_employee.emp_fname,
  tbl_employee.emp_mname,
  tbl_employee.emp_lname
FROM tbl_fdtr
INNER JOIN tbl_employee ON tbl_fdtr.emp_id = tbl_employee.emp_id
WHERE date(tbl_fdtr.in_out) >= '$from'
  AND date(tbl_fdtr.in_out) <= '$to'
  AND tbl_fdtr.emp_id= '$id'
GROUP BY date(tbl_fdtr.in_out)
  

我想得到$ val [&#39; timediffs&#39;]的日期总和;   我在时间表计算器中尝试了,我得到了结果&#39; 96:12:00&#39;。所以我想要那样的东西。

2 个答案:

答案 0 :(得分:0)

试试这个

$sum = 0;

foreach ($unique_date as $date) {
    $sum += strtotime($date['timediffs']);
}

$sumTime = date("H:i:s",$sum);

答案 1 :(得分:0)

$totalTimeInSecs = 0;
foreach ($unique_date as $timeArray) {   
    if (isset($timeArray['timediffs'])) { 
      list($hours,$mins,$secs) = explode(':',$timeArray['timediffs']);
      $totalTimeInSecs += (int) ltrim($secs,'0'); // Add seconds to total
      $totalTimeInSecs += ((int) ltrim($mins,'0')) * 60; // Add minutes to total
      $totalTimeInSecs += ((int) ltrim($hours,'0')) * 3600; // Add hours to total
    }  
}
$hours = str_pad(floor($totalTimeInSecs / 3600),2,'0',STR_PAD_LEFT);
$mins = str_pad(floor(($totalTimeInSecs % 3600) / 60),2,'0',STR_PAD_LEFT);
$secs = str_pad($totalTimeInSecs % 60,2,'0',STR_PAD_LEFT);
echo "Total time string: $hours:$mins:$secs";