1

I am trying to write a function that will add time stamps together to get a sum of all. For example:11:30 + 12:00 + 15:35 = 39:05 I am unsure of how to accomplish this. I have included code below that I have tried but it does not give the desired result:

$TotalTime = strtotime($data['TotalSunday']) + strtotime($data['TotalMonday']) 
    + strtotime($data['TotalTuesday'])
    + strtotime($data['TotalWednesday'])    + strtotime($data['TotalThursday'])
    + strtotime($data['TotalFriday']) + strtotime($data['TotalSaturday']);
    $data['TotalTime'] = gmdate("h:i", $TotalTime);
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Yamaha32088
  • 4,125
  • 9
  • 46
  • 97

4 Answers4

7

The problem is the following, you do want to add "times", but in fact you are creating the sum of "dates". strtotime expects two parameters: #1 is a date, #2 is a offset (if not given, it is "now").

This is merely a hack, and I'm not sure if anyone would use it like that, but it works:

<?php

    $time1 = strtotime("00:25Z", 0);
    $time2 = strtotime("23:20:05Z", 0);

    $sumTime = $time1 + $time2;

    $hours = $sumTime / 3600;
    $minutes = ($sumTime % 3600) / 60;

    echo sprintf("%d:%d", $hours, $minutes); // Outputs: 23:45

?>

By setting the second parameter to 0, and providing the datetime parameter with an additional Z (for Zulu time, or UTC), times up to 23:59:59 are parsed correctly in seconds.

So if you really want to rely on strtotime, use it like that.

Briareos386
  • 1,927
  • 18
  • 34
0

You can't use the old php strtotime/date functions when adding timings, especially if you want to present the result as only hours:minutes. For example, if you end up with more than 24 hours the "clock" will reset.

What you should do is calculate the number of total minutes (or seconds if that is necessary), then format the result as you wish.

Take a look at Convert number of minutes into hours & minutes using PHP for code examples of the latter part.

Community
  • 1
  • 1
Alasjo
  • 1,240
  • 8
  • 17
0

You can try something like this (I played around with DateTime and DateInterval for a bit, but be careful because these classes are only available in PHP 5.2+.

$interval1 = new DateInterval('P0000-00-00T10:00:00');
$interval2 = new DateInterval('P0000-00-00T11:00:00');
$interval3 = new DateInterval('P0000-00-00T03:45:00');
// format:                     YYYYY-MM-DDTHH:MM:SS

$date = new DateTime('00:00:00');
$date2 = clone $date;
$date2->add($interval1);
$date2->add($interval2);
$date2->add($interval3);

echo "Sum between the intervals:", $date2->diff($date)->format("%D:%H:%I"); // prints 01:00:45
// OR you can do the following
$diff = $date2->getTimestamp() - $date->getTimestamp(); // difference in seconds
// floor($diff / 3600) is the number of hours, for example
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
0

To avoid date change after 24 hours, I use this approach. It depends that the time format is always hh:mm(:ss) but no date before. Useful to work with mySQL date fields.

$timeString[] = array ('11:30', '1:00' , ...);

foreach ( $timeString as mytime) {
  $time         = explode(':' , $mytime);
  $tMin     +=   $time[1]   ;
  $tHour    +=   $time[0]   ;
}
    
$tHour  += floor ( $tMin / 60   ) ;
$tMin   =   $tMin % 60  ;

echo "total time: " . $tHour .':'.$tMin ;
user30424
  • 147
  • 1
  • 10