0

I know this question is asked already. But I can't think crystal clear how to deal with this. I already read some articles on how to deal with it. My problem is I have a website that deals with meeting/schedules but the user of the website may came from other country which is my big problem because they have different timezone.

Here is the situation.

I created a meeting tomorrow and set the time to 3:00 PM (considering that I'am in Singapore). My client will be in France so the time that he will see on the website should be 9:00 AM.

How can I come up with this?

Thanks for your help guys.

Imat
  • 500
  • 2
  • 4
  • 15
  • 1
    look at this if it helps http://www.php.net/manual/en/function.localtime.php – user3470953 May 07 '14 at 07:13
  • 1
    You can force the "locale" by reading some preference of the user, or via $_SERVER['HTTP_ACCEPT_LANGUAGE']: see date-default-timezone-set [https://php.net/manual/en/function.date-default-timezone-set.php] – gafreax May 07 '14 at 07:15
  • 1
    google: 'php convert time zones' may be useful. – Ryan Vincent May 07 '14 at 07:20

3 Answers3

1

Here's how I would go about it.

  1. Default timezone you set in your php script, it's whatever you decide it to be.

  2. Then, for display purposes of your users, you determine what's the timezone of your user. You do this using javascript, since you want the info from client side. Some possible solutions:

pure javascript https://stackoverflow.com/a/1091399/525445

jQuery https://stackoverflow.com/a/5607444/525445

(or even - combine these two approaches - get timezone with pure javascript, and then store that value in session variable, like in the jquery example).

3. Now that your php script knows your user's timezone, you can use it to convert your default timezone, to whatever time they have, using this:

https://stackoverflow.com/a/2505687/525445

Community
  • 1
  • 1
CodeVirtuoso
  • 6,318
  • 12
  • 46
  • 62
  • I've seen similar approach in PHPbb and maybe other bb scripts. You choose timezone while registering. My guess is that after login `ini_set()` is being called at the beggining of each request to set timezone for specific user. – ex3v May 07 '14 at 07:20
1

PHP contains a DateTime object that can be used to convert across timezones.

I'd suggest storing all timestamps in your database in the same timezone (or if that's not possible, then save the timezone along with the timestamp). When you need to display a timestamp, pull it from the database and then convert it to the user's local timezone.

$date = new DateTime('2000-01-01', new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:s'); // save this one to your database
// Output: 2000-01-01 00:00:00


$date->setTimezone(new DateTimeZone('Australia/Adelaide'));
echo $date->format('Y-m-d H:i:s'); // Serve this one to the user
// Output: 2000-01-01 09:30:00

Here is the full list of timezones supported by PHP.

Zoe
  • 2,129
  • 3
  • 21
  • 33
  • Thanks @Zoe, how can i know what is the current timezone of my user? – Imat May 07 '14 at 07:38
  • 1
    @Imat Here are a couple of ways: http://stackoverflow.com/questions/4746249/get-user-timezone – Zoe May 07 '14 at 23:36
0

Try this:

date_default_timezone_set("Asia/kolkata");
$date= date("Y-m-d H:i:s");
Dharman
  • 30,962
  • 25
  • 85
  • 135