Link to home
Start Free TrialLog in
Avatar of OmniUnlimited
OmniUnlimitedFlag for United States of America

asked on

Correct Time for Any Geographic Location Any Time of the Year

Hello Experts!

I am in the process of designing a website that displays a list of meeting dates and times throughout the year.  The problem is that this site has to be fully international and allow the visitor from any country to view these meeting dates and times translated to the time zone where the visitor lives.

Does anyone know of a dependable website that offers an API to be able to determine the correct time for any time zone given the date and time of a particular meeting, remembering that Daylight Savings Time varies from region to region?
SOLUTION
Avatar of duttcom
duttcom
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OmniUnlimited

ASKER

Hi duttcom,

Thank you so much for these references.  I do apologize for the delay in getting back to you, its just that I needed some time to investigate these sites and also wanted to see if any other experts might know of similar sites.

Do you or any other experts know of any other sites that are available for my purposes out there?

Thanks!
If you are using PHP, the datetime functionality is exceptionally accurate and dependable.  This article describes how it works.  See especially the value of the Unix Timestamp.  

Net takeaway message: You do not need an API if you use PHP!
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Now that said, if you really want an API, I can write one for you.  Just tell me the inputs you want to present and the outputs you want to receive.  I'll create a RESTful API.
SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Permatime pretty much distills it down to the UNIX timestamp, then repurposes the timestamp by applying a local time zone.  Looks good, but it's not reassuring that the author's link points here :-/
http://teamportfolios.com/?nr=0
should the calculation be done client- or server-side?
also: do you expect multiple calendar systems?
Here is a simple way to do it yourself.


<?php

echo "Hello<br><br>";

?>
<script language="javascript">
function formatDate(unix_timestamp) {
   var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
   var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
   var date = new Date( unix_timestamp * 1000 );
   var DayOfWeek = weekday[date.getDay()];
   var Month = months[date.getMonth()];
   var Day = date.getDate();
   var Hour =date.getHours();
   var Minutes = d2(date.getMinutes());
   var ampm = "am";
   if(Hour==12)  {
      ampm = "pm";
   }
   if(Hour==0)  {
      Hour = 12;
      ampm = "am";
   }
   if(Hour>12) {
      Hour = Hour-12;
      ampm = "pm";
   }
   else {
      Hour = d2(Hour);
   }
   document.write(DayOfWeek + ", " + Month + " " + Day + " at " + Hour + ":" + Minutes + " " + ampm);
}
function d2(n) { return (n<10)?"0"+n:n; }
</script>
<?


   $datetime = "2013-05-22 08:45:00";

   $year = substr($datetime,0,4);
   $month = substr($datetime,5,2);
   $day = substr($datetime,8,2);
   $hour = substr($datetime,11,2);
   $minutes = substr($datetime,14,2);
   $seconds = substr($datetime,17,2);

   $unix_timestamp = mktime($hour,$minutes,$seconds,$month,$day,$year);

   echo "<script> formatDate(\"$unix_timestamp\");</script>";
   echo "<font size='-2'>&nbsp;&nbsp;(your local time)</font>";

exit();
?>

Open in new window

Experts, thank you very much for your participation.  You assistance is greatly appreciated.

@woolmilkporc:  Thank you for fixing the link.  It looks interesting, and I will check it out.

@Ray_Paseur:  Ray, it is always a pleasure to see your participation in my questions.  You always provide some very interesting and unique insights.  

I am intrigued by the claim you make concerning the UNIX timestamp.  As always, the article you wrote was quite informative.

I have several questions for you, however.  Daylight Savings Time is a legislated time construct that varies from country to country and from time zone to time zone.  How is it possible that a server in any particular country could keep track of the precise moment when these times change in any part of the world?  Wouldn't something like that have to be programmed?  And if such a thing is programmed into PHP, what happens if the the law changes (like what happened recently here in the United States) and you are still running the same version of PHP?

And that sparks another question.  Daylight Savings Time changes (at least here in the United States) don't fall on the same dates each year.  How does the system know when is the right date?  Is this a manual change done by the IT experts on the server?  And if so, who makes the changes for say, Pakistan?

Of course, I know the UNIX timestamp will never change, but my question extends to the accuracy of the PHP time zones.

This is extremely important to my client, as her meetings are established world wide, and she absolutely must have accurate meeting information at all times.

Another question related to this is, people find the meetings on her site by typing in their city, state or region, and country.  With this information, how would I easily determine which PHP timezone I would apply?  Would this require yet another API?

@ahoffmann:  We need to try and keep everything as server-side as possible.

@yodercm:  Again, I am looking for server-side solutions.
> ... keep everything as server-side as possible.
and
>  ... This is extremely important to my client

then the first question is: how do you get the intended time and calender format from the client? I mean get it reliably

second, what is your server-side programming language
@ahoffmann:  Sorry, but I'm a little confused.  I refer to both the work station and to my customer as "clients".  To which are you referring?

My customer inputs her meeting information through the administrative controls of her website.  The site is built in PHP and the date/time information is translated into a UNIX timestamp and stored within the database.  When a visitor comes to the site and wants to attend one of the online meetings, she can plug in her location information into the site and the site will generate a list of meetings with the meeting times translated to match the time zone of the visitor.
PHP needs to be kept up-to-date, as does any program.  If you're reasonably up-to-date then the issues related to time zone locale are not going to be any trouble.  Examples abound, but here are a few that I know PHP handles correctly.

Daylight savings time transitions, even though they differ by region and year, work correctly.  Consider Mountain time here in the US, and the special case of Phoenix, AZ.  There is an example in the article about it, excerpted here.
<?php // RAY_temp_foo.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// TEST FOR LEAP YEAR AND DST
if (date('L')) echo "NOW IT IS A LEAP YEAR";
if (date('I')) echo "NOW IT IS DAYLIGHT SAVINGS TIME";

// SHOW THE CUTOVER FROM DST TO STANDARD TIME
$a = date('c', strtotime('November 7, 2010 1:57am'));
$z = date('c', strtotime('November 7, 2010 2:02am'));
while ($a < $z)
{
    $a = date('c', strtotime($a . '+ 1 minute'));
    echo "<br/>$a ";
    if (date('I', strtotime($a))) echo "IS DAYLIGHT SAVINGS TIME";
}

Open in new window

Install that and run it to see how it works.

You can turn location data into timezones with the public and free API here:
https://developers.google.com/maps/documentation/timezone/

You get 2,500 requests per day.  If you geocode your client's city-state location information, you can put the geocoded information into the TZ API.  With a little program logic you can probably build a data base of the 10,000 most populous areas in 4 or 5 days.

To be sure there are some wrinkles that would suggest you might need to ask the (human) client to verify the timezone you're selecting for them.  In India, for example, there are some timezones that vary by 15 minutes.  Russia has 11 zones, I think, whereas China has 1.  If you're scheduling airlines for arrival and departure this sort of think is important.  And it seems to work fairly well.
@Ray_Paseur:  Thank you Ray.  As my client runs her site on a dedicated server, I know we can keep up-to-date with PHP updates.  My only hope is that PHP updates keep up with changes in legislation throughout the world.

The Google TZ API you suggested returns timezones in the format of "Pacific Daylight Time", etc., and not into the country/city or country/state/city, etc. format of PHP.

Is there any way to be able to figure out what the closest PHP timezone would be to any particular location?
If you are on a Linux server, the normal updates include the Olsen TZ-data updates which PHP uses to determine time zones.  And the local clock is normally set to UTC-0 (old GMT) time and the current time zone is used to make local time correct.  

Hosted servers, however, often do not do any updates so they don't break anyone's code.  I still have at least one client on PHP 4.4.
You said:  @yodercm:  Again, I am looking for server-side solutions.

I don't exactly understand why this isn't.  You have to have some kind of input from  your client side to know what location or time zone they are in, and that is all that my solution does.  It still runs on your server.
@DaveBaldwin: Interesting information.  Thanks!

@yodercm: I'm not saying your solution is bad.  It's just that the core of your calculations is based on javascript, a client side, not server side, scripting language.
Not sure, since you seem to be limited in what you can do (all server side) but I'll suggest -
That when they submit the (what you call) location, that there is Also a required entry box for a "Time zone", if you can get that (a time zone), then your problem may be solved, you might consider a drop-down Select box, with all (or most) of the PHP timezones, and let them pick the one they want, then just maybe you could use this -
date_default_timezone_set($_POST['timeZone']);
and get some appropriate time.

Back a short while, i was involved in trying to get some info (country, timezone, etc.) from an address (location), if the address is in the "Usual" there are web retrieves for that, however if it is in the "Un-Usual" then on these you get unpredictable results. Outside the more metro (city) areas you can get many "Un-Usual" addresses. I would really suggest trying to Let the User "pick" or indicate (Hours from GMT or UTC) to get a more accurate time assessment.

Also you might consider returning the "Time" to the browser as a UTC time, and then use javascript to re-factor it (UTC time) to the users browser's local time. Although this does use javascript, the actual time spec. comes from the server, and you can store all database "Times" as in a UTC.
Maybe there are other ways, but trying to get a time zome from address may not be as easy or dependable as just using a web API ? ?
This site http://www.worldtimezone.com/ shows the difficulty of getting the correct local time around the world.
Slick812: I'll have to check with my client to see if she would consider adding that option to her form.

As far as the suggestion to take the browser time, that is tempting.  Unfortunately, it has been my experience that many computer system times are simply wrong, and if it's wrong, I'm not going to get an accurate timezone.

@DaveBaldwin: Again, useful information.  Thanks!
My javascript solution will give the time based on the user's computer.  Most computers these days are synched to web time, so they are accurate.  In any case, if a user has a wrong time on his/her computer, they will know it and adjust if necessary.
@yodercm: When you say "Most computers these days are synched to web time" are you considering the entire international market, including third world countries which may operate very antiquated systems?  Also, as surprising as it seems, there are people who get on computers that have no idea how to adjust the system time.
@OmniUnlimited, the users local time and the GeoIP of the ISP are the only two things you have to work with.  If either or both are wrong, there's nothing you can do to fix that.  Even if you have them enter their location, most won't know the 'official' time zone that they are in.
Well, I think you are too hung up on "server-side" when the solution is very simple.  But ... I've given you code that works.  Good luck.
@DaveBaldwin:  Thanks Dave.  Yes, I know that.  That's why I am resistant to using system times to figure out the time zone and would prefer to use the visitor's address to try and figure it out.

Thinking that a third party site set up specifically for the purpose of keeping track of all the times in the world would be more accurate than trusting the latest version of PHP made me ask the Experts about their knowledge concerning any good, highly reliable API sites.

Ray's comments were very interesting, and spurred more questions, however.  Now I also need to know if there is an easy way of getting a location matched with its respective PHP timezone.

Thanks!
@yodercm: We can agree to disagree on that one.

You supplied me with working code for something I didn't ask for.  The original question was: "Does anyone know of a dependable website that offers an API to be able to determine the correct time for any time zone given the date and time of a particular meeting, remembering that Daylight Savings Time varies from region to region?"

What you gave me was neither a website, nor an API.
As long as you understand that there is no 'clean' or perfect answer.  There is nothing to 'guarantee' that the user's ISP is even in the same country much less the same time zone.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will no doubt need to have a GMT Time, shown beside any "Local Time", no matter how you arrive at the "Local Time", as you have stated = "may operate very antiquated systems", , so analytical local  TIME output that comes from SERVER calculation , may have some inaccuracies , due to antiquated this or that. But my limited knowledge (of the third world countries tech) opinion, is to have the browser have some javascript output from a UTC. But trying get an address location from "third world countries" with adjunct address formats may also be limited.
The Google TZ API you suggested returns timezones in the format of "Pacific Daylight Time", etc., and not into the country/city or country/state/city, etc. format of PHP.
While this statement is partially true, it is incomplete and inaccurate about what is omitted.  The API returns several data elements, including everything you need to set the PHP timezone directly in PHP programming.  Example:

For Washington, DC, the API returns "America/New_York" which is the correct designation for prevailing Eastern time.

Learn more about it here:
http://www.php.net/manual/en/function.date-default-timezone-set.php
http://www.php.net/manual/en/timezones.php
http://www.php.net/manual/en/timezones.america.php
@Slick812:
... trying get an address location from "third world countries" ...
Ha!  And if you think it's hard to get that, just wait till you try to get payment in standard currency.

:-)

~Ray
@DaveBaldwin: Thanks Dave, but I've learned long ago that we live in a far from perfect world.  The only thing we can do is to try and make it better.

@Ray_Paseur:  Thanks again Ray, but you didn't answer my question.  I mentioned that the Google TZ API you suggested returns timezones in the format of "Pacific Daylight Time", etc., and not into the country/city or country/state/city, etc. format of PHP.

Is there any way to be able to figure out what the closest PHP timezone would be to any particular location?   You suggested proximity calculations.  So does that mean that I need to geocode the entire PHP time zone list so that I can do these calculations?

@Slick812: Thank you again Slick.  As I've stated in different ways several times, I do not want to be dependent on sistem times for our calculations.  The best solution I have seen so far is to ask the user to tell us where they live.  It has been our experience that people don't generally lie about that because then it screws up their meeting times.
LOL!  Outstanding Ray.  You know I completely glossed over the timeZoneId Time Zone Response element in the Google documentation.

You are as always, a super genius!
Thanks for the points and thanks for using EE.  Going forward, it's quite all right to share the points among those who contribute to the answers ;-)

What would you pay for a license to use an API that did exactly what you wanted?  eg: You pass it a location and it gives you back a complete response about the current time in that location.  Or you pass it a location and a DATETIME value and it returns a Unix Timestamp, etc...?
Oh shoot!  In all the excitement I forgot.  You are absolutely right Ray.  There are two other experts I need to divide points with who provided the solutions I asked for.

Sorry Ray, I need to call attention to this.
Thanks again to everyone for their participation, and thanks to the great Community Support people for getting the points thing squared away.