Skip to content
Advertisement

Adding DateTime intervals with foreach loop

I asked a question yesterday in adding together datetime intervals and was pointed to this thread – How we can add two date intervals in PHP

This is great and makes sense. However, when I try to do what the submitted answer says in a foreach loop, I’m ending up with an incorrect result.

This is a function I have made that gets all the clock in times and out times of staff, they are stored in the db and are created using PHP’s date(H:i:s).

My function gets all the in and out times of any given employee, and my reporting feature I’m working on needs to display the total amount of hours they have worked.

I tried to achieve this by converting the times to datetime objects and using ->diff to get the intervals and thus calculating that days hours, I am then trying use a foreach loop to add the intervals together thus giving me a sum total of the hours worked in any given date range.

The whole function together is this:

JavaScript

I was hoping to get a monthly total, however it seems I’m only getting one days total as my final result. Here is a screen shot of the UI (the total hours are crudely circled) this also shows the time stamps my function should be adding together.

1

Advertisement

Answer

You can do this in a single query instead. Use TIMEDIFF() to get the difference for each row, then convert those to seconds by using TIME_TO_SEC(), SUM() those up and put it back into time-format with SEC_TO_TIME() – all in MySQL!

JavaScript

Making your function with a prepared statement..

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement