0

I'm trying to use start time and end time captured in these variables. I'd like to add these values together to get an overall time spent on variouse tasks. So I got these values and am trying to do something like this:

//start times
$time_1 = $_POST['start_of_service_1'];
$time_2 = $_POST['start_of_service_2'];
$time_3 = $_POST['start_of_service_3'];

//end times
$etime_1 = $_POST['end_of_service_1'];
$etime_2= $_POST['end_of_service_2'];
$etime_3 = $_POST['end_of_service_3'];

//total hours
$tot_hours_1 = date('H:i',strtotime($etime_1)-strtotime($time_1);
$tot_hours_2 = date('H:i',strtotime($etime_2)-strtotime($time_2));
$tot_hours_3 = date('H:i',strtotime($etime_3)-strtotime($time_3));

$totaltime = $tot_hours_1 + $tot_hours_2 + $tot_hours_3

So if the time spans are each 5 minutes, it would be 00:05 + 00:05 + 00:05 = 00:15 minutes total.

These time functions seem so tricky I'm having awful troubles and time spent to overcome this obstacle.

Can anyone give me advice on how to accomplish this?

John Kress
  • 21
  • 1
  • I recommend to use DateTime, similiar question : http://stackoverflow.com/a/2767124/1611108 – pce Feb 19 '13 at 19:09
  • You cant add a date formatted time to another date formatted time using +. I don't think that would return what you expect. Why not just get all of the differences using, `$tot_hours_1 = strtotime($etime_1) - strtotime($time_1);` then simply add all the $tot_hours_1/2/3 together. THEN use the date function to format the final result. – Tristan Feb 19 '13 at 19:09

2 Answers2

2

Just remove the date('H:i', and corresponding ). This will leave you with numbers, which can be added together. Formatting with date() should be the last thing you do.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thanks everyone. I ended up using the 2nd solution above. In the end I then converted tot hours to decimal. – John Kress Mar 19 '13 at 16:45
0

You are trying to add date formats in this line $totaltime = $tot_hours_1 + $tot_hours_2 + $tot_hours_3. Time formats cannot be added using +. Try adding just pure numbers and then format it using date().

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17