General discussion

Locked

PHP working time

By Choppit ·
I'm trying to write an efficient PHP function to calculate the working minutes elapsed between two timestamps. (By working minutes I mean not including weekends or time outside the range 08:30 to 17:30). The function I've written works but is way too slow for the 2000 or so timestamp pairs it will be required to process for each execution of the calling script.

Anyone got any ideas?

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Tony Hopkinson In reply to PHP working time

I've seen this done 'cept I haven't got access to the machine anymore.
Doing it in the DB will be way quicker than in PHP.
What database is it. ?
Asuuming start and end cannot be non working days You calc the working time for the first day and the last day. Then the number of full days between the first and last . knock off weekends and holidays and then * 540 and add to the two earlier results. Most DMBS's can do this very quickly. with a few loops and a hols table with their built in datetime arithmetic.

the datediff and dayofweek functions arre usually implemented

Collapse -

by Choppit In reply to

Thanks Tony, It took me five or six attempts rewriting from scratch to eliminate (known) errors which came about as a result of unlikely conditions but I now have something I'm reasonably happy with. Its now processing 20,000 records in < 5 secs and is mostly implemented in PHP.

Collapse -

by Choppit In reply to PHP working time

Thanks for the reply Tony, DB is MySQL V5.0. I've not as yet had any experience of implementing flow control in DBMS so I guess this should be my next port of call, perhaps using a stored procedure?

Collapse -

by Tony Hopkinson In reply to PHP working time

Stored procedure will give you some more optimisation. But flow control and being able to declare and use variables should do the bulk of it.
With the date arithmetic functions, you should be off and running.

Collapse -

by Choppit In reply to

Poster rated this answer.

Collapse -

by Choppit In reply to PHP working time

This question was closed by the author

Back to Web Development Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums