[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Performing Time Calculations for Time Zones

Posted on 2008-06-17
7
Medium Priority
?
196 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
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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
Course of the Month19 days, 18 hours left to enroll

872 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