Reputation: 1321
I want to calculate total minute with my data.
[{"id":3,"zone_id":21,"created_at":"2017-06-22 09:49:33","updated_at":"2017-06-22 09:49:33","deviceNo":"ogrv", "type": "0" },{"id":4,"zone_id":22,"created_at":"2017-06-22 09:50:09","updated_at":"2017-06-22 09:50:09","deviceNo":"ogrv", "type": "1"},{"id":5,"zone_id":21,"created_at":"2017-06-22 09:50:35","updated_at":"2017-06-22 09:50:35","deviceNo":"ogrv", "type": "0"},{"id":6,"zone_id":22,"created_at":"2017-06-22 11:51:52","updated_at":"2017-06-22 11:51:52","deviceNo":"ogrv", "type": "1"},{"id":7,"zone_id":21,"created_at":"2017-06-22 11:54:57","updated_at":"2017-06-22 11:54:57","deviceNo":"ogrv", "type": "0"},{"id":8,"zone_id":22,"created_at":"2017-06-22 11:57:17","updated_at":"2017-06-22 11:57:17","deviceNo":"ogrv", "type": "1"}]
if type 0 will come I will start to calculate until type 1. How can I calculate it with php?
Each type means different actions so when different actions came I need the calculate minutes between this 2 data.
For example First Data came with type 0 that means user is inside then second data is came with type 1 and that means user is not inside. I need the calculate total minutes between this datas.
Upvotes: 0
Views: 161
Reputation: 9369
Look at this, May help you
If you have database table model as Model,
$data = Model::all()->toArray();
$diff_array = array();
for($i=0; $i<count($data)-1; $i++){
$first = $data[$i];
$second = $data[$i+1];
if($first['type']==0 && second['type'] == 1){
// Calculate difference between $first['created_at] and $second['created_at'] as $diff_time
// you can parse string date using Carbon and calculate diffrence.
array_push($diff_array,$diff_time);
}
else if($first['type']==1 && second['type'] == 0){
// Calculate difference between $first['created_at] and $second['created_at'] as $diff_time
array_push($diff_array,$diff_time);
}
}
// dump the array to view differences
dd($diff_array);
Upvotes: 1