onlyican Posted June 2, 2013 Share Posted June 2, 2013 Hi All I am having some issues with Timezones. Basically I have an internal application where someone can go on and log date / time start and finish. It is stored in my DB as 2013-06-02 09:51:00 2013-06-02 12:01:00 I then have a report to show these times I have set date_default_timezone_set('UTC'); And I loop through the hours and give a total hours worked that day $shift_hours = strtotime($logoff) - strtotime($logon) echo date('H:i', $shift_hours); Works fine. With the above example, it says I done 2 hours 10 minutes. But My Issue: I want a total hours worked. Locally, this works fine but on my server its way out. I simply add the total hours up $total_hours = $total_hours + $shift_hours; Now working this out, I have an issue I have a small function [php $total_num_hours = floor($total_hours / (60*60)); $total_num_hours_in_mins = $total_num_hours * (60*60); $total_mins = ($total_hours - $total_num_hours_in_mins) / 60; return array($total_num_hours, $total_mins); [/php] As I said, locally this works fine, I done 45 Hours on my example data, live i have worked 470 odd hours. (Although that seems nice and makes me look good, Im sure people will moan) I cant do a simply echo date('H:i'... as if they have worked 25 hours, that will display 1 (as it counts as one day) I dont really want to calculate the number of days worked, the number of months worked to make hours. Is there an easier / better way Cheers Quote Link to comment https://forums.phpfreaks.com/topic/278690-timezone-issue-for-hours-shift-report/ Share on other sites More sharing options...
cpd Posted June 2, 2013 Share Posted June 2, 2013 You could do it completely with SQL. Something along the lines of: SELECT SUM(TIMESTAMPDIFF(SECOND, `start`, `finish`)) `total_seconds` FROM `table_name` You can then format the seconds with something like date("H\hrs i\m", $seconds). Quote Link to comment https://forums.phpfreaks.com/topic/278690-timezone-issue-for-hours-shift-report/#findComment-1433721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.