Jump to content

Recommended Posts

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
 
 

 

Link to comment
https://forums.phpfreaks.com/topic/278690-timezone-issue-for-hours-shift-report/
Share on other sites

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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.