Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 679
  • Last Modified:

URGENT! Timezone Conversion

As the title goes, how do you, for example, convert a timestamp to a specific timezone?

thanks in advance,
gam3r_3xtr3m3
0
gam3r_3xtr3m3
Asked:
gam3r_3xtr3m3
  • 4
  • 3
  • 3
  • +2
2 Solutions
 
Diablo84Commented:
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
 
merwetta1Commented:
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
 
iceboxmanCommented:
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
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!

 
merwetta1Commented:
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
 
iceboxmanCommented:
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
 
merwetta1Commented:
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
 
Diablo84Commented:
All seem over complicated comparred to strtotime.
0
 
Oliver_DornaufCommented:
Yes Diabolo84 - is is more comlicated and I also it wasn´t the question.
0
 
iceboxmanCommented:
Much better code merwetta.  :)
0
 
SkonenCommented:
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
 
iceboxmanCommented:
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
 
Diablo84Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now