0

I have time data in mySQL in the format of %Y-%m-%d %H:%M:%S as UTC +0 format. And I want to display the time data adjusting for the local computer tha user is using.

For example: The data is 2012-05-29 15:00:00 Then the time should be shown to a user in East time 2012-05-29 11:00:00 (applying daylightsaving, UTC -4hour) and for the westcoast should displayed 2012-05-20 08:00:00 (URC -7hour)

Any scripts for converting this? either PHP or Javascript is good.Thanks!

clerksx
  • 632
  • 4
  • 13
  • 21

2 Answers2

0

Store all your dates internally using UTC and then for each user set:

if ( $_SESSION['user_timezone'] ) date_default_timezone_set($_SESSION['user_timezone']);

If you want to be brave and your users are internet based (as opposed to using VPN like most of mine) you can lookup their location using location services.

I store this per user and just let them pick.

Easy way to generate the timezone list to present to the user:

echo '<select id="userTimezone" onChange="some_ajax_function">
        <option value="">Please select your Timezone</option>';
foreach (DateTimeZone::listIdentifiers() as $value )
{
    if ( $_SESSION['user_timezone'] == $value )
    {
        $output .= '<option value="' . $value . '" selected="selected">' . htmlentities($value) . '</option>';
    }
    else
    {
        $output .= '<option value="' . $value . '">' . htmlentities($value) . '</option>';
    }
}
$output .= '</select>';
Daren Schwenke
  • 5,428
  • 3
  • 29
  • 34
0

Here is a quick example using PHP DateTime class.

$time = '2012-05-29 15:00:00';

$datetime = new DateTime($time, new DateTimeZone('UTC'));
$datetime->setTimezone(new DateTimeZone('America/Los_Angeles'));

echo $datetime->format('Y-m-d H:i:s'); // 2012-05-29 08:00:00

Date and Time Classes

drew010
  • 68,777
  • 11
  • 134
  • 162