Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Try this:

date_default_timezone_set("Asia/kolkata");
$date= date("Y-m-d H:i:s");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top