Solved

Performing Time Calculations for Time Zones

Posted on 2008-06-17
7
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:virmaior
ID: 21806386
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
ID: 21806954
Unfortunately that option does not work and is something that was tried. Any other ideas?
0
 
LVL 20

Expert Comment

by:virmaior
ID: 21807014
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:Tluckett
ID: 21807190
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
ID: 21807368
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
ID: 21815975
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
ID: 21818624
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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