SQL: Offsetting timestamp in a query from GMT to Pacific
I'm hoping someone can help out a SQL newbie (note, I'm very green so please be gentle). I'm trying to help do an audit of some contractor employees at my company and see if they might be milking their hours on their reported time sheets. They work remotely so we don't see them come & go. What I'm running is a query to see the first and last timestamps in their entries and then roll them up by day so I can get an idea of the number of hours they're spending doing work on a given date. So the query I'm running looks something like this:
SELECT MIN(entry_time) AS first_time_stamp, MAX(entry_t开发者_开发技巧ime) AS last_time_stamp, employee
FROM some_database.some_table
WHERE employee = 'Kevin' AND entry_time > '20110301'
GROUP BY DATE_FORMAT(entry_time, '%Y%m%d'), employee
;
This returns some data that looks like the following sample.
'2011-03-01 01:10:58', '2011-03-01 17:53:43', 'Kevin'
'2011-03-02 12:00:47', '2011-03-02 20:36:59', 'Kevin'
'2011-03-03 01:34:58', '2011-03-03 21:37:22', 'Kevin'
So far so good, except that you'll notice that "Kevin" seems to be working some really long days. The issue is that the timestamps are in GMT and we're on the west coast in the US. So for example, the first time stamp that shows up would've actually been on February 28th and might have been his last entry of that day.
I've spent the past day Googling for ways to offset these timestamps by -08:00 before they're selected but keep coming up empty or confused (usually both). Anyone out there know how I might be able to offset the entry_time
so that it's returning values based on the offset time?
Note that I don't really need to worry about Standard vs Daylight Saving times since I'm just really looking for the relative hours worked.
Thanks!
The function http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_convert-tz converts timestamps between timezones and should do what you need. (I assume that you're using MySQL.)
精彩评论