Solved

PHP Worldwide Times Using DateTime Objects

Posted on 2014-02-20
12
338 Views
Last Modified: 2014-02-20
Hello Experts!

Another quick question for you PHP experts out there.  I know I can use DateTimeZone::listIdentifiers() to obtain a list of all the time zones available to PHP, and I can use DateTimeZone::getTransitions() to obtain neat info for each time zone like offsets, daylight savings time, etc.

My question is that once I have the offset for each time zone, is there a way to display the current time for each time zone, such as
America/Los Angeles (9:03 AM PST)

Open in new window

?

Any help would be greatly appreciated.  I just need the code that will actually calculate the correct time based on the provided offset or time zone.  Bonus points to the most elegant solution!
0
Comment
Question by:OmniUnlimited
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39874224
This and much more is shown in the article here (look for the code snippet with "Timezones Around the Globe"):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

See date_default_timezone_set().  Easy!
0
 
LVL 17

Author Comment

by:OmniUnlimited
ID: 39874286
Hi Ray!

I knew I could count on you for a great answer.  Unfortunately, this question is really meant to help me understand (teach me, if you will) how I can work with times from different time zones WITHOUT changing the default time zone for the server using the DateTime classes (DateTime, DateTimeImmutable, DateTimeInterface, DateTimeZone, etc.)  Are you familiar with the use of the PHP DateTime classes after PHP v.5.2?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39874378
Yes, I am familiar with DateTime objects.  Just curious - why would you not want to change the default timezone?  You can get it and set it pretty easily.

If you want to give me some examples of things you would like to do, I'll be glad to show you some demonstration scripts.
0
 
LVL 17

Author Comment

by:OmniUnlimited
ID: 39874409
The site has a bunch of asynchronous stuff going on, some of which are dependent upon the system time.  I don't want a system wide resetting of the time zone to mess these up.

I am just looking for the shortest, cleanest solution to display the proper time next to its respective time zone.  As I mentioned earlier, I can iterate through the array supplied by the DateTimeZone::listIdentifiers() method to get each time zone, then I can use the DateTimeZone::getTransitions() method to get the neat information like offset, daylight savings time, time zone abbreviation, etc.  Having this information available (offset, name of time zone), I want a statement that can display the actual time for this time zone.

I hope this makes sense.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39874445
Changing the "default time zone" changes it for your Page, not for the server.  You can change it as often as you want.  Linux servers are normally set to use GMT or whatever it's called now.

https://php.net/manual/en/timezones.others.php
0
 
LVL 17

Author Comment

by:OmniUnlimited
ID: 39874477
@DaveBaldwin:  Yeah, I was wondering about that.  The PHP manual states that date_default_timezone_set sets the default timezone used by all date/time functions in a script.  Obviously, this would include all included scripts, am I not right?  Would this affect scripts that are called from that script via AJAX?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39874586
Would this affect scripts that are called from that script via AJAX?
That doesn't actually make any sense.  You have to remember the separation of code.  PHP runs on the server and sends the page to the browser and then it's Done.  Any scripts contacted thru AJAX are 'new' in the sense that they are loaded when the AJAX call is made.  They don't 'know' anything about the original PHP script that was used to load the page.  If you need a 'default timezone' in the script called by AJAX, it has to be set in that script.  It does not in any case carry over from the PHP script that was used to load the page in the first place.
0
 
LVL 17

Author Comment

by:OmniUnlimited
ID: 39874605
@DaveBaldwin: :P You know I've been working long hours, and I guess I am not thinking very clearly this morning.  You are absolutely right.  The asynchronous scripts would not be affected by a change in the default time zone settings.

I still would be curious to see the solution using DateTime objects, however.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39874661
I still would be curious to see the solution using DateTime objects, however.
I have no idea what that means.  If you look at this page, you will see that most Date/Time functions are actually aliases of the Date/Time classes.

http://us3.php.net/manual/en/book.datetime.php

And in this page, it says that you will get a 'notice' if the 'default timezone' has not been set before the functions are used.

http://us3.php.net/manual/en/function.date-default-timezone-set.php

And this page shows the two different 'styles':

http://us3.php.net/manual/en/datetime.settimezone.php
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39874790
How about this
<?php
// GET OUR OFFSET FROM GMT (OR UCT)

$datetime = new DateTime();
$offset = $datetime->getOffset();

// GET THE CURRENT TIME 

$theTime = time();

// GET LIST OF TZ's AND LOOP THROUGH THEM

$times = DateTimeZone::listIdentifiers();
foreach($times as $t) {

  // CREATE A NEW DateTimeZone OBJECT WITH THE CURRENT IDENTIFIER
  
  $tz = new DateTimeZone($t);
  
  // GET THE TRANSITIONS FOR THE CURRENT TIME - WILL RETURN A 
  //SINGLE ELEMENT ARRAY
  
  $trans = $tz->getTransitions($theTime, $theTime);
  
  // WORK OUT THE TIME FOR THE LOCATION BY TAKING THE CURRENT 
  // TIME (UCT) AND ADDING THE OFFSET FOR THE LOCATION AND 
  // SUBTRACTING OUR LOCAL OFFSET
  
  $time = strtotime($trans[0]['time']) + $trans[0]['offset'] - $offset;
  
  // FINALLY OUTPUT THE RESULT
  
  echo "$t (" . date('h:i:s a', $time) . ")<br/>";
}
?>

Open in new window

0
 
LVL 17

Author Closing Comment

by:OmniUnlimited
ID: 39874849
That's perfect!  Thanks.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39875023
You are welcome - thanks for the poitnts.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now