Summing time in DateTime() objects - php

I have an array of DateTime() objects in which I store some time duration (like "H:i")
How can I sum all elements in this array to get total time duration?
And it's also should be taken into account, that if total time duration is grater that 23:59, I have to be able to get nubmer of days.
is this possible?
I was trying to do like this:
$duration = new DateTime('00:00');
foreach($routes as $route) {
$arrival_time = new DateTime();
$arrival_time->setTimestamp($route->arrival_time);
$departure_time = new DateTime();
$departure_time->setTimestamp($route->departure_time);
$leg_duration = $arrival_time->diff($departure_time);
$duration->add($leg_duration);
}
but in $duration I got wrong time.
P.S.
Using $duration->add($leg_duration); I got subtracted from "24:00" time, why?
For example if $leg_duration = new DateTime('02:10'); the result will be "21:50".
$duration->sub($leg_duration); add time to "24:00"
Is this right?

Concerning part of the question: you're using the diff() method in the inverted way, the code should actually be:
$leg_duration = $departure_time->diff($arrival_time);
Because in your code you are calculating $departure_time - $arrival_time (which is negative, thus giving you this "inverted" result).
Concerning the addition of time intervals, as a DateTime object contains a date too, you might have to do something like this to get a meaningful result:
$start = new DateTime('00:00');
$duration = $start;
// ... your loop ...
$total = $start->diff($duration);
$total will be a DateInterval object, whose fields should contain your total time difference (although I didn't test this).

echo sum_the_time('01:45:22', '17:27:03');
this will give you a result:
19:12:2
function sum_the_time($time1, $time2) {
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time)
{
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
}
$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes = floor($seconds/60);
$seconds -= $minutes*60;
// return "{$hours}:{$minutes}:{$seconds}";
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); // Thanks to Patrick
}
I found from here

Related

Difference between durations

I am creating a timesheet whereby it shows expected and actual hours.
The durations are saved like the below
23:15 - 23 hours and 15 mins
25:45 - 25 hours and 45 mins
I need to work out the difference in hours and mins between the two (extra hours worked)
I have tried the below
$acutal=='23:15';
$expected=='25:45';
$start_time = new DateTime("1970-01-01 $acutal:00");
$time = $start_date->diff(new DateTime("1970-01-01 $expected:00"));
This does work, however when the hours are over 24:00 it throws an error (obviously because it's reading it as time)
Uncaught exception 'Exception' with message 'DateTime::__construct():
Failed to parse time string (1970-01-01 25:45:00)
Is there another way to do this?
You could check if the number of hours are greater than 24, and if so, add a day, and remove 24 hours.
$actual='23:15';
$expected='25:45';
$day = 1;
list($hrs, $min) = explode(':', $expected);
if ($hrs > 24) { $day += 1; $hrs -= 24; }
$start_time = new DateTime("1970-01-01 $actual:00");
$time = $start_time->diff(new DateTime("1970-01-$day $hrs:$min:00"));
echo $time->format('%hh %Im');
Output:
2h 30m
Please also note that == is used to compare, not to assign.
You can also change the if ($hrs > 24) by while(), if there is 48 hours or more.
edit
As pointed out by #CollinD, if the time exceed the number of days of the month, it will fail. Here is another solution:
$actual='23:15';
$expected='25:45';
list($hrs, $min) = explode(':', $actual);
$total1 = $min + $hrs * 60;
list($hrs, $min) = explode(':', $expected);
$diff = $min + $hrs * 60 - $total1;
$start_time = new DateTime();
$expected_time = new DateTime();
$expected_time->modify("+ $diff minutes");
$time = $start_time->diff($expected_time);
echo $time->format('%hh %Im');
You can do it manually by keeping track of the number of minutes worked - this will be exact and will also allow you to show negative differences.
<?php
// get the difference in H:mm between two H:mm
function diff_time($actual, $expected) {
$diff_mins = mins($actual) - mins($expected);
return format_mins($diff_mins);
}
// convert a HH:mm to number of minutes
function mins($t) {
$parts = explode(':', $t);
return $parts[0] * 60 + $parts[1];
}
// convert number of minutes into HH:mm
function format_mins($m) {
$mins = $m % 60;
$hours = ($m - $mins) / 60;
// format HH:mm
return $hours . ':' . sprintf('%02d', abs($mins));
}
var_dump(diff_time('23:15', '25:45'));
var_dump(diff_time('25:15', '23:45'));
This outputs:
string(5) "-2:30"
string(4) "1:30"
.. first, 2:30 less than expected, for the second 1:30 more than expected.
You can try using datetime functions but it seems a lot more straightforward to me to treat the times as string, use split or explode to get hours and minutes, convert to integers, get the difference in minutes and convert it back to hours and minutes (integer divide by 60 and remainder).
$t1=explode(':',$expected);
$t2=explode(':',$actual);
$d=60*($t1[0]-$t2[0])+t1[1]-t2[1];
$result=str_pad(floor($d/60),2,'0',STR_PAD_LEFT).':'.str_pad($d%60,2,'0',STR_PAD_LEFT);

Adding multiple times PHP

I am building an online timesheet for employees. At the end of the timesheet I need to show the total hours:mins worked.
I have the hours worked as variables:
$MondaysActual
$TuesdaysActual
$WednesdaysActual
$ThursdaysActual
$FridaysActual
The data held is something like 07:20 - the employee worked 7 hours 20 mins
How can I simply add these 5 times together and show the total as hrs:mins?
Try this
$times = array();
$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";
// pass the array to the function
echo AddPlayTime($times);
function AddPlayTime($times) {
$minutes = 0; //declare minutes either it gives Notice: Undefined variable
// loop throught all the times
foreach ($times as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
// returns the time already formatted
return sprintf('%02d:%02d', $hours, $minutes);
}

How can I sum multiple time in laravel 5.2

For example, I have multiple times like:
$a = "9:00:00";
$b = "8:00:00";
$c = "9:00:00";
and so on...
It must return in 26:00:00 but how to subtract it from 45:00:00?
I have to find out total working hours and overtime for my attendance.
You can figure out the different using Carbon. Need some little trick to add these times. You can do it this way:
$a = '09:00:00';
$b = '08:00:00';
$c = '09:00:00';
//convert the $a in carbon instance.
//convert $b and $c in integer, you can add only integer with carbon.
$d = Carbon::createFromFormat('H:i:s',$a)->addHours(intval($b))->addHours((intval($c)));
//convert the time "45:00:00" to carbon
$e = Carbon::createFromFormat('H:i:s','45:00:00');
//return the difference
$e->diffInHours($d)
You can do the following:
$sumSeconds = 0;
foreach($times as $time) {
$explodedTime = explode(':', $time);
$seconds = $explodedTime[0]*3600+$explodedTime[1]*60+$explodedTime[2];
$sumSeconds += $explodedTime;
}
$hours = floor($sumSeconds/3600);
$minutes = floor(($sumSeconds % 3600)/60);
$seconds = (($sumSeconds%3600)%60);
$sumTime = $hours.':'.$minutes.':'.$seconds;
This is the code for suming the three times (supposed that they are in array) and the code for subtracting will be almost the same but for the subtraction you will subtract $sumSeconds of both times and then convert the result.
Carbon is what you need, it's integrate with laravel by default.
http://carbon.nesbot.com/docs/
using its addHours() and subHours() function to achieve your requirements.
$times = [
"9:00:00",
"8:00:00",
"9:00:00",
];
// Converting the time to seconds makes calculations
// more simple and easier to understand.
function timeToSeconds($time) {
list($hours, $minutes, $seconds) = explode(":", $time);
return ($hours * 60 * 60) + ($minutes * 60) + $seconds;
}
// Let's use this to convert say 300s into 00:05:00
function formatSecondsAsHMI($seconds) {
return sprintf(
'%02d:%02d:%02d',
floor($seconds / 3600),
floor($seconds / 60 % 60),
floor($seconds % 60)
);
}
// Add an array of times together and return the formatted string hh:mm:ss
function addTimes($times) {
$seconds = array_sum(array_map(function ($time) {
return timeToSeconds($time);
}, $times));
return formatSecondsAsHMI($seconds);
}
// Subtract an array of times. Order of array important.
// Subtracts 0 from 1 from 2 where 0,1,2 are array keys
// i.e. [03:00:00, 10:00:00] would subtract 3 from 10 = 07:00:00
function subtractTimes($times) {
$times = array_map(function($time) {
return timeToSeconds($time);
}, $times);
return array_reduce($times, function($carry, $item) {
return ($item - $carry);
});
}
// Now just add the times together and subtract the result from 45
echo subtractTimes([addTimes($times), '45:00:00']);

php sum time with variable in while loop

php sum variable in while loop
I have to "sum" variable's values in while, here us my example :
while($row = mysql_fetch_array($result)){
$working_hour= $row[working_hour];
}
The code above will output if I put echo $working_hour; for example:
01:00:03, 01:03:04, 01:10:15
I want something like : sum($working_hour) or array_sum($working_hour) to count all the results of while loop. So, that i want to count: 01:00:03, 01:03:04, 01:10:15= 03:13:22
I try this way :
$total_working_hour=’00:00:00’;
while($row = mysql_fetch_array($result)){
$working_hour= $row[working_hour];
$total_working_hour+= $working_hour;
}
Echo $total_working_hour;
The code above provide output as:
03
How can I do it with php?
Thanks
$hours=0;$min=0;$sec=0;
while($row = mysql_fetch_array($result)){
$working_hour= $row[working_hour];
$arr=explode(':',$working_hour);
$hours=$hours+$arr[0];
$min=$min+$arr[1];
if($min>60){$hours++;$min=$min-60;}
$sec=$sec+$arr[2];
if($sec>60){$min++;$sec=$sec-60;}
}
echo 'Total working hours='.$hours.':'.$min.':'.$sec;
I used the answer here (how to sum the array time) and created the following function:
function addTime($a, $b) {
$array = [$a, $b];
$totalTimeSecs = 0;
foreach ($array as $time) { // Loop outer array
list($hours,$mins,$secs) = explode(':',$time); // Split into H:m:s
$totalTimeSecs += (int) ltrim($secs,'0'); // Add seconds to total
$totalTimeSecs += ((int) ltrim($mins,'0')) * 60; // Add minutes to total
$totalTimeSecs += ((int) ltrim($hours,'0')) * 3600; // Add hours to total
}
$hours = str_pad(floor($totalTimeSecs / 3600),2,'0',STR_PAD_LEFT);
$mins = str_pad(floor(($totalTimeSecs % 3600) / 60),2,'0',STR_PAD_LEFT);
$secs = str_pad($totalTimeSecs % 60,2,'0',STR_PAD_LEFT);
return "$hours:$mins:$secs";
}
So you can use this and replace
$total_working_hour+= $working_hour;
with
$total_working_hour = addTime($total_working_hour, $working_hour);
The value of $row["working_hour"] is clearly a string. So saying something like "01:00:03" + "01:03:04" clearly makes no sense. PHP assumes that what you meant to do was cast the strings to integers first and then add them together. The result of that is not what you're actually after.
Instead, you want to convert a string like "01:00:03" to an normalized integer value, like number of seconds, that can be added together and then converted back to a string value.
So to get the normalized value of the string as an integer in seconds you need a function like this...
function convertStringToNumSeconds($string) {
list($hours, $minutes, $seconds) = explode(":", $string, 3);
$totalSeconds = 0;
$totalSeconds += $hours * 3600; // There are 3600 seconds in an hour
$totalSeconds += $minutes * 60; // There are 60 seconds in a minute
$totalSeconds += $seconds; // There is 1 second in a second
return $totalSeconds;
}
Then to convert the seconds back to a formatted string you can do the opposite...
function secondsToString($seconds) {
$hours = (int) floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = (int) floor($seconds / 60);
$seconds -= $minutes * 60;
return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}
Now in your loop you can actually do something like this...
$totalWork = 0;
while($row = mysql_fetch_array($result)){
$totalWork += convertStringToNumSeconds($row["working_hour"]);
}
echo secondsToString($totalWork); // You should get your expected result
If the format in your example is fix, you can work with DateTime-Object and Date-Interval as well like this... (for further information to DateInterval, have a look at the PHP-Docu)
$dt = new \DateTime('00:00:00');
foreach ($dbRows as $row) {
$time = $row['working_hour'];
$timeSplit = explode(':', $time);
$interval = new \DateInterval(
'P0Y0DT'.$timeSplit[0].'H'.
$timeSplit[1].'M'.$timeSplit[2].'S'
);
$dt->add($interval);
}
echo $dt->format('H:i:s'); // Output: 03:13:22

Get difference between two specific number of hours using PHP

I wanna get the difference between specific number of hours, as I'm working on payroll project which requires to get total working hours of an employee.
let's say the employee has worked for 40:18:20 (hh:mm:ss)
And he missed to work for 12:15:10 (hh:mm:ss)
I want to get the difference between those two times as following:
(40:18:20) - (12:15:10) = (28:03:10)
Is it possible via PHP functions?
What I actually did, is to split that as string, and tried to subtract each number individually and then recollect them again, which is "as I think" is not professional.
Please advise.
you can use this function
function getTimeDiff($dtime,$atime){
$nextDay=$dtime>$atime?1:0;
$dep=explode(':',$dtime);
$arr=explode(':',$atime);
$diff=abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
//Hour
$hours=floor($diff/(60*60));
//Minute
$mins=floor(($diff-($hours*60*60))/(60));
//Second
$secs=floor(($diff-(($hours*60*60)+($mins*60))));
if(strlen($hours)<2)
{
$hours="0".$hours;
}
if(strlen($mins)<2)
{
$mins="0".$mins;
}
if(strlen($secs)<2)
{
$secs="0".$secs;
}
return $hours.':'.$mins.':'.$secs;
}
this will handle hours greater then 24.
$start = date_create(gmdate('D, d M Y H:i:s',timeTosec('40:18:20')));
$end = date_create(gmdate('D, d M Y H:i:s',timeTosec('12:15:10')));
$diff=date_diff($end,$start);
print_r($diff);
function timeTosec($time){
sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
return $time_seconds;
}
This will handle differences greater than 24 hours.
Code is maybe a bit too broken down, but on the other hand it is easy to understand.
// Use any valid date in both cases
$a = new DateTime(date("Y-m-d H:i:s", mktime(5, 20, 15, 12, 31, 2016)));
$b = new DateTime(date("Y-m-d H:i:s", mktime(65, 10, 5, 12, 31, 2016)));
$c = $a->diff($b);
$days = $c->format("%a");
$hours = intVal($c->format("%H")) + intVal($days);
$minutes = $c->format("%m");
$seconds = $c->format("%s");
// Unformatted result
print $hours . ':' . $minutes . ':' . $seconds;
Another solution with mktime:
$diff return a timestamp then you need to convert it in hh:mm:ss
$diff = mktime(40,18,20)-mktime(12,15,10);
$hours = $diff/3600 %3600;
$minutes = $diff/60 %60;
$seconds = $diff % 60;
$time= $hours.":".$minutes.":".$seconds;

Categories