Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT! Timezone Conversion

Posted on 2004-08-31
15
Medium Priority
?
677 Views
Last Modified: 2009-01-09
As the title goes, how do you, for example, convert a timestamp to a specific timezone?

thanks in advance,
gam3r_3xtr3m3
0
Comment
Question by:gam3r_3xtr3m3
[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
  • 3
  • +2
15 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 1000 total points
ID: 11948886
you could use strtotime, example

<?php
$time = strtotime("+ 1 hour"); //the timestamp
echo date("H:i:s",$time);
?>

if you wanted it to be dynamic (eg. selecting the time difference via a drop down) you could do

<?php
$offset = $_POST['drop_down_name'];
$time = strtotime("+ $offset hours");
echo date("H:i:s",$time);
?>

note the offset would be the offset from your servers time zone
0
 
LVL 6

Assisted Solution

by:merwetta1
merwetta1 earned 1000 total points
ID: 11949114
PHP has a timezone environment variable that you can modify with the putenv() function. Then any calls to the date function will give time in that timezone.

<?
echo "Original Time: ". date("h:i:s")."\n";
putenv("TZ=America/Los_Angeles");
echo "New Time: ". date("h:i:s")."\n";
?>

You just need to replace the "America/Los_Angeles" string with whatever timezone you want from the following list (timezone strings are the 3rd item in each row): http://www.theprojects.org/dev/zone.txt
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 11949164
I like to use this code:

$timezone = -8;

$now_arr = getdate();
$now = mktime($now_arr['hours'] + $timezone, $now_arr['minutes'], $now_arr['seconds'], $now_arr['mon'], $now_arr['mday'], $now_arr['year']);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:merwetta1
ID: 11949241
OK, I'm going to give you a complete solution that will tell you the date and time in any timezone and adjust it for daylight savings:


<html>
<body>
<?

if ($_POST['offset'])
{
      ## use gmdate to get GMT offset by correct amount for chosen timezone
      ## date("I") is used to adjust for daylight savings (it returns 1 for daylight savings, 0 for not)
      echo "The date and time in that timezone is ".
               gmdate("m/j/Y H:i:s", time() + 3600*($_POST['offset']+date("I")));
} else
{
?>
      <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
      Tell me the time for: <select name="offset">
       <option value="-12">(GMT -12:00) Eniwetok, Kwajelein
       <option value="-11">(GMT -11:00) Midway Island, Samoa
       <option value="-10">(GMT -10:00) Hawaii (Lucky You!!)
       <option value="-9">(GMT -09:00) Alaska (Brrr..Cold)
       <option value="-8">(GMT -08:00) Pacific Time (US &amp; Canada), Tijuana
       <option value="-7">(GMT -07:00) Mountain Time (US &amp; Canada)
       <option value="-6">(GMT -06:00) Central Time (US &amp; Canada)
       <option value="-5">(GMT -05:00) Eastern Time (US &amp; Canada)
       <option value="-4">(GMT -04:00) Atlantic Time (Canada Only)
       <option value="-3">(GMT -03:00) Brasilia, Buenos Aries
       <option value="-2">(GMT -02:00) Mid Atlantic (See land yet?)
       <option value="-1">(GMT -01:00) Azores, Carpe Verdes Island
       <option value="0" selected>(GMT 00:00) Greenwich Mean Time: Dublin, London
       <option value="+1">(GMT +01:00) Belgrade, Prague, Brussels, Paris
       <option value="+2">(GMT +02:00) Athens, Istanbul, Cairo, Israel
       <option value="+3">(GMT +03:00) Moscow, St Petersburg, Kuwait
       <option value="+4">(GMT +04:00) Baku, Tblisi
       <option value="+5">(GMT +05:00) Ekaterinburg, Islamabad, Karachi
       <option value="+6">(GMT +06:00) Almaty, Colombo, Dhaka
       <option value="+7">(GMT +07:00) Bangkok, Hanoi, Jakarta
       <option value="+8">(GMT +08:00) Beijing, Singapore, Hong Kong, Taipei
       <option value="+9">(GMT +09:00) Yakutsk (wherever the hell that is)
       <option value="+10">(GMT +10:00) Brisbane, Canberra, Sydney
       <option value="+11">(GMT +11:00) Magadan, Solomon Islands
       <option value="+12">(GMT +12:00) Auckland, Wellington, Fiji
      </select>
      <input type="submit" name="submit">
      </form>
<?
}
?>
</body>
</html>
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 11949573
You didn't include places like Arizona where DST is not observed.  If you do a complex solution like that, you will need to research which locations observe DST.  Also, you never quite gave it in a timestamp.
0
 
LVL 6

Expert Comment

by:merwetta1
ID: 11950139
sheesh, iceboxman! Your code doesn't give a timestamp, either.    8-)

Here is a revised solution that addresses iceboxman's criticisms:

<html>
<body>
<?

if ($_POST['offset'])
{
      ## use gmdate to get GMT offset by correct amount for chosen timezone
      ## date("I") is used to adjust for daylight savings (it returns 1 for daylight savings, 0 for not)
      if ($_POST['dst'] == 1)  $dst_offset = date('I');
      else $dst_offset = 0;
      echo "The date and time in that timezone is ".
            gmdate('m/j/Y H:i:s', time() + 3600*($_POST['offset'] + $dst_offset));
      echo "<br>and the Unix timestamp is ".
            gmdate('U', time() + 3600*($_POST['offset'] + $dst_offset));
      echo "<br>and the SQL timestamp is ".
            gmdate('YmdHis', time() + 3600*($_POST['offset'] + $dst_offset));
} else
{
?>
      <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
      Tell me the time for: <select name="offset">
      <option value="-12">(GMT -12:00) Eniwetok, Kwajelein
      <option value="-11">(GMT -11:00) Midway Island, Samoa
      <option value="-10">(GMT -10:00) Hawaii (Lucky You!!)
      <option value="-9">(GMT -09:00) Alaska (Brrr..Cold)
      <option value="-8">(GMT -08:00) Pacific Time (US &amp; Canada), Tijuana
      <option value="-7">(GMT -07:00) Mountain Time (US &amp; Canada)
      <option value="-6">(GMT -06:00) Central Time (US &amp; Canada)
      <option value="-5">(GMT -05:00) Eastern Time (US &amp; Canada)
      <option value="-4">(GMT -04:00) Atlantic Time (Canada Only)
      <option value="-3">(GMT -03:00) Brasilia, Buenos Aries
      <option value="-2">(GMT -02:00) Mid Atlantic (See land yet?)
      <option value="-1">(GMT -01:00) Azores, Carpe Verdes Island
      <option value="0" selected>(GMT 00:00) Greenwich Mean Time: Dublin, London
      <option value="+1">(GMT +01:00) Belgrade, Prague, Brussels, Paris
      <option value="+2">(GMT +02:00) Athens, Istanbul, Cairo, Israel
      <option value="+3">(GMT +03:00) Moscow, St Petersburg, Kuwait
      <option value="+4">(GMT +04:00) Baku, Tblisi
      <option value="+5">(GMT +05:00) Ekaterinburg, Islamabad, Karachi
      <option value="+6">(GMT +06:00) Almaty, Colombo, Dhaka
      <option value="+7">(GMT +07:00) Bangkok, Hanoi, Jakarta
      <option value="+8">(GMT +08:00) Beijing, Singapore, Hong Kong, Taipei
      <option value="+9">(GMT +09:00) Yakutsk (wherever the hell that is)
      <option value="+10">(GMT +10:00) Brisbane, Canberra, Sydney
      <option value="+11">(GMT +11:00) Magadan, Solomon Islands
      <option value="+12">(GMT +12:00) Auckland, Wellington, Fiji
    </select><br>
      <input type="checkbox" name="dst" value="1"> Check here to adjust for Daylight Savings<br>
      <input type="submit" name="submit">
      </form>
<?
}
?>
</body>
</html>

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11951188
All seem over complicated comparred to strtotime.
0
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 11952525
Yes Diabolo84 - is is more comlicated and I also it wasn´t the question.
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 11954000
Much better code merwetta.  :)
0
 
LVL 4

Expert Comment

by:Skonen
ID: 11960972
Heh. Being that everybody is posting just about every solution under the sun. Assuming the timestamp is based on Greenwich Mean Time, you could use something like:

function get_timezone ($timestamp, $offset){
      return $timestamp+(3600*(int)$offset);
}

It is simple, robust, and much more efficient then strtotime(). An example of usage would be:

$newtime = get_timezone($oldtime, -8);
gmdate("m/d/Y H:i:s", $newtime);

This example can of course be refined to:

gmdate("m/d/Y H:i:s", get_timezone($oldtime, -8));

Hope this helps!
           Stuart Konen
0
 
LVL 4

Expert Comment

by:iceboxman
ID: 11977123
You know, I like Skonen's code better than mine.  I'm changing mine to his, but with a little modification:

function get_timezone ($timestamp, $offset, $is_dst){
      return $timestamp+(3600*(int)($offset + $is_dst));
}

$is_dst should be 0 or 1.
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12526539
All that is needed for timezone conversion is strtotime. merwetta1's post is useful in the sense that it gives the time offsets for other zone so i propose split:

Diablo84  & merwetta1
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

718 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