Solved

Performing Time Calculations for Time Zones

Posted on 2008-06-17
7
187 Views
Last Modified: 2010-03-19
I am having a problem with performing time calculations. My Server runs on ET (Eastern Time) However we have a few customers that are not in that time zone. All of the times that go into the database are all in ET. What I am trying to do is pull data and do a time calculation in the query to collect the correct records.

In the code included the startT and endT columns are time type fields.

The thinking behind the query is that if I add 3 hours to the current server time it will pull the correct records for people in california. i.e. if its 1 in california the server thinks its actually 4 so I need to pull records and add 3 hours to what the database has recorded so that I can compare it to the current time on the server.

I hope this makes sense.
SELECT users.firstname, 

	users.lastname, 

	day_planner.UID,

	date_format(day_planner.endT, '%h:%i %p') as ending

FROM 

users 

LEFT OUTER JOIN day_planner ON users.UID = day_planner.UID

WHERE 

	startD = CURDATE() 

	and 

	CURTIME() BETWEEN TIME(DATE_ADD(day_planner.startT, INTERVAL + 3 HOUR)) AND TIME(DATE_ADD(day_planner.endT, INTERVAL + 3 HOUR))

Open in new window

0
Comment
Question by:Tluckett
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:virmaior
Comment Utility
This might not achieve what you are hoping for, but one option would be setting the timezone for your west coast people.

http://dev.mysql.com/doc/refman/4.1/en/time-zone-support.html

SET timezone = 'US/Western';

at the beginning of each connection from them.
0
 

Author Comment

by:Tluckett
Comment Utility
Unfortunately that option does not work and is something that was tried. Any other ideas?
0
 
LVL 20

Expert Comment

by:virmaior
Comment Utility
I guess I would need to better understand what is currently happening here.

(1) Is this script being called from a webpage or a cron job?
(2) How are the users who are in california marked in the DB?

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Tluckett
Comment Utility
Currently the Query is not returning results based on the query included here.

All the times are in the day_planner table and are stored in ET time. The users time zone is specified in an include file and the value of that variable is them placed into the query using a +/- # to indicate if their timezone is plus hours or minus hours of ET. This is something that is set up manually when we set up an account.
0
 
LVL 20

Expert Comment

by:virmaior
Comment Utility
I am not sure why your query isn't working without looking at your data.  but why bother storing the values offset from the actual time?
0
 

Author Comment

by:Tluckett
Comment Utility
The system was set up this way and was not intended to be used in multiple time zones. Now that it is we have to find a work around that should be simply accomplished in the query. The data is pretty straight forward

The startT and endT fields are both MYSQL Time fields formatted as 11:30:00, 15:45:00 etc. All I need is for the values in startT and endT to be added by say three hours and see if the Current time is between those times.
0
 

Accepted Solution

by:
Tluckett earned 0 total points
Comment Utility
I have found the solution to the problem which was to use the ADDTIME() function
SELECT users.firstname, 

	users.lastname, 

	day_planner.UID,

	date_format(day_planner.endT, '%h:%i %p') as ending

FROM users LEFT OUTER JOIN day_planner ON users.UID = day_planner.UID

WHERE 

	startD = CURDATE() 

	AND 

	CURTIME() BETWEEN 

		ADDTIME(day_planner.startT, '03:00:00') 

	AND 

		ADDTIME(day_planner.endT, '03:00:00')

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now