473,770 Members | 5,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tough Timezone Problem

I guess I'm confused by the whole timezone thing and I don't want to
write a web app that gets this stuff wrong. I don't know how timezones
work exactly -- is it just hours +/- on GMT or are there going to be
minor differences on minutes and seconds too?

What I want to do is have a web app that one logs into (great place for
submitting the browser's view on time via Javascript) and then the user
sees the records from the database with dates converted from GMT to
their local timezone. The database, however, will only store GMT time
for all the records. (Imagine a simplistic, one-table helpdesk
ticketing system for the style of database to make this easy. Oh, and
I'm more a fan of PostgreSQL than MySQL.)

How do I write such a thing? Let me know. Here's my rendition, if I
have this right.

If it's only just hours, then I guess I need my L1.php (login1) page --
which submits to L2.php for processing before showing Q.php for the
helpdesk queue -- to collect the browser's view on time via Javascript.
This is then stuck in a hidden field via Javascript and that field is
submitted to L2.php. L2 reads this and compares this with gmdate('Y-m-d
H:i:s'), checking only for the difference in hours. When it knows this,
then all the records (open tickets) it finds in the database have date
math applied to be this difference in hours. Even at that, I'm not too
skilled with applying date math in PHP. I didn't find a DateAdd or
DateDiff set of functions.

I'm also a Javascript Minimalist so that this doesn't break in certain
people's browsers. That's why all I was planning to do was collect the
browser's sense of date/time and pass that to L2.php so that the
processing can be consistent, rather than rely on Javascript to get it
right.

And then there's also the issue where you have a date in one timezone,
and the the previous or next day in the other timezone, so at that
point it becomes more than just hours, but changes in days too.

Jul 17 '05 #1
9 2134
Google Mike <go********@hot pop.com> wrote:
What I want to do is have a web app that one logs into (great place for
submitting the browser's view on time via Javascript) and then the user
sees the records from the database with dates converted from GMT to
their local timezone. The database, however, will only store GMT time
for all the records. (Imagine a simplistic, one-table helpdesk
ticketing system for the style of database to make this easy. Oh, and
I'm more a fan of PostgreSQL than MySQL.)


Take the KISS approach: leave _everything_ in GMT. That way you only
have to tell the user his offset to GMT or even more simply just tell
him the time in GMT when the page was loaded.

Jul 17 '05 #2
Yes, I have thought about that, and I do *LOVE* the KISS approach, and
I thank you for your extremely prompt reply (30 minutes is fantastic!).
However, I'm afraid my users might be too cranky to accept having to
translate GMT in their heads. People in different timezones (Brazil and
USA) will "live" in my web app all day, so they'll want it to be a
little more comfortable for them. Got another suggestion?

Jul 17 '05 #3
After getting the time and date from the browser into PHP, I would
convert it to a timestamp and compare it with the timestamp from the
server -- that will tell you the difference in seconds, which solves
your problem of crossing dates. Check out the strtotime() function. I
would take this difference in seconds, divide by 3600 and round to the
nearest integer. That will tell you the difference in hours, and also
avoid cases where a couple minutes of seperation between clocks would
otherwise throw things off.

I don't know about Postgres, but in MySQL you can definitely retrieve
dates and times as UNIX timestamps directly from the database. With a
timestamp, making an adjustment is easy, because you just add or
subtract the correct number of seconds. Using PHP's date() function
will allow you to format a timestamp into something more readable.

As a side note, strtotime() will also let you do date math if you give
it a timestamp as your second argument (or relative to the current time
if you don't). For instance:

strtotime("+1 day");
strtotime("next Thursday");
strtotime("-5 hours");

Jul 17 '05 #4


ZeldorBlat wrote:
After getting the time and date from the browser into PHP, I would
convert it to a timestamp and compare it with the timestamp from the
server -- that will tell you the difference in seconds, which solves
your problem of crossing dates. Check out the strtotime() function. I
would take this difference in seconds, divide by 3600 and round to the
nearest integer. That will tell you the difference in hours, and also
avoid cases where a couple minutes of seperation between clocks would
otherwise throw things off.
not that easy: india and nepal for instance have a 15 min difference in
their times (which is purely a political thing), and there might be
other countrys as well.

I don't know about Postgres, but in MySQL you can definitely retrieve
dates and times as UNIX timestamps directly from the database. With a
timestamp, making an adjustment is easy, because you just add or
subtract the correct number of seconds. Using PHP's date() function
will allow you to format a timestamp into something more readable.

As a side note, strtotime() will also let you do date math if you give
it a timestamp as your second argument (or relative to the current time
if you don't). For instance:

strtotime("+1 day");
strtotime("next Thursday");
strtotime("-5 hours");


Jul 17 '05 #5
Google Mike <go********@hot pop.com> wrote:
Yes, I have thought about that, and I do *LOVE* the KISS approach, and
I thank you for your extremely prompt reply (30 minutes is
fantastic!). However, I'm afraid my users might be too cranky to
accept having to translate GMT in their heads. People in different
timezones (Brazil and USA) will "live" in my web app all day, so
they'll want it to be a little more comfortable for them. Got another
suggestion?


Translating timezones is extremly easy:
news:41******** *************** @dreader3.news. xs4all.nl
(althought the IST timezone used there is BS, putenv is the answer to
your display question :)

Jul 17 '05 #6
You can use JavaScript, or ask the user for their current time. THe
difference between GMT and the given time is their time offset. I
store this in my user DB, but you could easily put it in a cookie or
somesuch. With that time offset, every time you display a date, be sure
to correct by their offset.

People really do appreciate being able to see all times local.

~D

Jul 17 '05 #7
chotiwallah wrote:

not that easy: india and nepal for instance have a 15 min difference in
their times (which is purely a political thing), and there might be
other countrys as well.


Australia has 3 time zones in winter and up to 5 in summer. Tasmania,
Victoria, NSW, and Queensland are all in the same time zone. However
Tasmania changes to summer time a month before Vic, or NSW, while
Queensland does't use summer time. They also change back to standard
time on different dates.
Jul 17 '05 #8
I may have a routine below that works. It depends on the browser having
the proper representation of the time and all else falls into place. If
the browser doesn't have the proper representation of time, including
special summer/winter time adjustments and political time adjustments,
then that's a matter to complain to the browser vendor, in my opinion.
I mean, I can't work miracles here. (One suggestion is to always
provide a preferences setting in

<!-- login1 page (L1.php) -->
<html><head><ti tle>Login p1</title>
<script language="javas cript">
function GetTime() {
var now = new Date();
return now;
}
function UpdateHidden(My Form) {
thedate = GetTime()
MyForm.fldUsert ime.value=theda te;
return true;
}
</script></head><body>
<form name="main" id="main" method="post" action="L2.php"
onSubmit="javas cript:UpdateHid den(this)">
<p>The date in your locale is:
<script language="javas cript">
document.write( GetTime());
</script>
</p><input type=hidden name="fldUserti me" id="fldUsertime " value="">
<input type="submit" value="Login"></form></body></html>

<!-- login2 page (L2.php) -->
<!-- THIS IS SUPPOSED TO BE A SEPARATE FILE (L2.php) -->
<?php
function GetLocalTime($d BrowserDateTime ) {
return date('Y-m-d H:i:s', time() + strtotime($dBro wserDateTime));
}
function GetTimezoneOffs et($dUserDateTi me) {
return (strtotime($dUs erDateTime)-strtotime(gmdat e('Y-m-d
H:i:s')))/3600;
}
function TranslateByTime zoneOffset($dDa teTime,$nTZO) {
return date('Y-m-d H:i:s',strtotim e($dDateTime)+( $nTZO*3600));
}
//sub main
$dUsertime=$_PO ST['fldUsertime'];
$dUsertime=GetL ocalTime($dUser time);
$nTZO = GetTimezoneOffs et($dUsertime);
$dTest = '2005-01-28 07:15:00';
$dResult = TranslateByTime zoneOffset($dTe st,$nTZO);
$dGMT = gmdate('Y-m-d H:i:s');
echo "Your browser tells me the time is: $dUsertime<BR>\ n";
echo "And the web server's concept of GMT is: $dGMT<BR>\n";
echo "Your Timezone Offset is: $nTZO<BR>\n";
echo "It would be a good idea to store the Timezone Offset in a
cookie.<BR>\n";
echo "<P>So if I had a GMT date in the database that was
<B>$dTest</B>,&nbsp;";
echo "you would see it as <B>$dResult</B> if you used the&nbsp;";
echo "TranslateByTim ezoneOffset() routine and passed it the Timezone
Offset&nbsp;";
echo "cookie value.</P>\n";
echo "<A HREF='javascrip t:history.back( )'>Go Back</A>";
?>
<html><head><ti tle>Login p2</title></head><body></body></html>

Jul 17 '05 #9
Got cutoff there when I wanted to finish the sentence for "(One
suggestion is to always provide a preferences setting in..."

My suggestion is that one could use the above routine, but then always
provide an override setting where one can change their +/- setting on
the GMT with a SELECT/OPTION field and store that in a persistent
cookie.

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1362
by: Jason Kinkade | last post by:
I recently installed MySQL 4.0.18 on my slackware 9.1 machine (kernel 2.4.25) by compiling the source. It works fine, and all tests pass except one. The timezone test. Can someone tell me why I'm getting this error? Is there something with the TZ variable I have to set for the shell? I think its EDT right now. timezone Errors are (from /custompath/mysql/mysql-test/var/log/mysqltest-time)
7
3204
by: Jim Davis | last post by:
I'm (still) working on an ISO 8601 date parser. I want to convert at least the formats described here: http://www.w3.org/TR/NOTE-datetime Well.. I've got most of it working (via RegEx's) good enough for me but I'm having a brain block when it comes to TimeZone. The datetime may come with an optional timezone offset (from GMT) as here (the offset is +1 hour from GMT):
1
2553
by: Raj Chudasama | last post by:
I have the following code for a clock in my gui. Everything works fine EXCEPT the following line when the TimeZone is changed in the windows. string tz = TimeZone.CurrentTimeZone.StandardName; It will not pick the changed timezone but it does pick up time change due to timezone change.
6
2480
by: Bijoy Naick | last post by:
I have an events table which stores the time of each event - the time and assoicated timezone. Is there a way of converting this time into GMT (with support for DST).. some sort of function which accepts a time and timezone as parameters and returns the time in GMT?
0
3219
by: helloitsme | last post by:
Hello All! I know there already exists a thread with this topic by Mike! But my problem did not get solved with that.... I have a working code in VB6 that can set the selected timezone for the system. When i converted that code in VB .NET, I am having a problem similar to what Mike was having. When I select my timezone to Eastern Time Zone (or SA Pacific), my code sets my system timezone to 'Bogota, Lima, Quito'. i guess it is becoz both...
2
11855
by: Sanjay | last post by:
Hi All, Using pytz, I am facing a problem with Asia/Calcutta, described below. Asia/Calcutta is actually IST, which is GMT + 5:30. But while using pytz, it was recognized as HMT (GMT + 5:53). While I digged into the oslan database, I see the following: # Zone NAME GMTOFF RULES FORMAT Zone Asia/Calcutta 5:53:28 - LMT 1880 # Kolkata
2
5173
by: Michele Locati | last post by:
Hi to everybody I'm working with dates and time, and I can't find the way to determine the time in a generic TimeZone. Here's an example of what I'd like to do: // I get the local date and time DataTime NowHere = DateTime.Now;
2
3686
by: =?Utf-8?B?Sm9ubnk=?= | last post by:
I have an ASP.NET 2.0 C# web application that is contacting an Exchange server using WEBDAV. It allows the users to look up appointments for a future date. The problem I have is determining the correct client time zone. Note that Exchange stores everything in UTC time. I am able currently able to use javascript to ask the client what time zone they are in, but this is only for the "today" (the current day) and not for a day in the...
6
20326
by: jehugaleahsa | last post by:
Hello: I would like it so that the time displayed was always for CDT/CDS. Potentially, the application can be run at locations in Mountain Time or Central Time. This is what I have now: // get the UTC offset depending on day light savings double hourOffset = DateTime.Now.IsDaylightSavingTime() ? -5 : -6; DateTimeOffset offset = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(hourOffset));
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.