?
Solved

Performing Time Calculations for Time Zones

Posted on 2008-06-17
7
Medium Priority
?
194 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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