Solved

URGENT! Timezone Conversion

Posted on 2004-08-31
15
670 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
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 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 250 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
 
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
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 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

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

Title # Comments Views Activity
Online file editor, manager 6 59
Password forgotten. 3 40
simplest php form 3 59
Put radio button in my form but already has components from my database 4 44
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

919 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

16 Experts available now in Live!

Get 1:1 Help Now