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?

2
  • I recommend to use DateTime, similiar question : stackoverflow.com/a/2767124/1611108 Commented Feb 19, 2013 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. Commented Feb 19, 2013 at 19:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone. I ended up using the 2nd solution above. In the end I then converted tot hours to decimal.
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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.