If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > best approach for calculating work hours?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-19-09, 04:44
ferdna ferdna is offline
Registered User
 
Join Date: Oct 2003
Posts: 6
best approach for calculating work hours?

i have a table with three fields (id, date, time) sample data

1, 01/01/2009, 08:00:00
2, 01/01/2009, 11:00:00
3, 01/01/2009, 13:00:00
4, 01/01/2009, 18:00:00

this data equals 8 hours for that one day.

i want to be able to calculate how many hours per day, should i do this on mysql or on my programming? or what is the best way of doing this?


thank you.
Reply With Quote
  #2 (permalink)  
Old 10-19-09, 05:27
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by ferdna
what is the best way of doing this?
Redesign your table. At the moment it's difficult to work out which time is a start time and which is an end time. Then it's difficult to work out which end times go with which start times. The structure also doesn't allow you to cross a date boundrary ie start at 11pm and finish at 7am. I also believe putting time and date into separate fields also creates more issues than it solves.
Reply With Quote
  #3 (permalink)  
Old 10-19-09, 05:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
[QUOTE=ferdna]this data equals 8 hours for that one day./quote]wha????

perhaps you should do this with application programming
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 10-19-09, 11:03
ferdna ferdna is offline
Registered User
 
Join Date: Oct 2003
Posts: 6
mike_bike_kite, how could i re-design it?

r937, yeah i was thinking doing in code not sql.
Reply With Quote
  #5 (permalink)  
Old 10-19-09, 11:37
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by ferdna
mike_bike_kite, how could i re-design it?
Perhaps a table like this:
Code:
create table WorkTimes (
   id                       int,
   start_time           datetime,
   end_time             datetime null
);

-- your test data
insert WorkTimes values ( 1, '2009-01-01 08:00:00','2009-01-01 11:00:00' );
insert WorkTimes values ( 1, '2009-01-01 13:00:00','2009-01-01 18:00:00' );
I'm not sure what the times actually relate to - are they times that various people have worked or what projects have been worked on? I put a random id field in there anyway. Something like the following will then show how many hours were worked on a given day. If there is no end_time set then it assumes you're still working.
Code:
select sum( 
           unix_timestamp( ifnull( end_time,now() ) ) - 
           unix_timestamp( start_time )
          ) / 3600
from WorkTimes
where date( start_time ) = '2009-01-01';
You can lookup the functions sum, unix_timestamp and ifnull.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On