0

In india if i save time at around 2017-05-09 10:12:21 in database by php date(), mysql NOW(), and by timestamp in three column .So it produces the result

date()                      NOW()            timestamp

2017-05-09 04:38:37 | 2017-05-08 22:38:37 | 2017-05-08 22:38:37

Now I want to convert this time to local time according to worldwide users timezone . how I can do this.Please forgive for any mistake I am new in php.

2 Answers2

0
$date = new DateTime($yourTimestamp, new DateTimeZone($timezone));
echo $date->format('Y-m-d H:i:sP');

Where $yourTimestamp is the timestamp in local time, and $timezone is timezone you want to convert the timestamp to.

Timezones: http://php.net/manual/en/timezones.php

Enstage
  • 2,106
  • 13
  • 20
0

According to this, if your mysql column is of type TIMESTAMP, the date time is stored in UTC: it will be converted from local timezone while inserting and retrieving.

To check what the default timezone for MySQL is, try this:

SELECT @@system_time_zone;

PHP's date function returns the formatted date string from the current unix timestamp. It will use the current default timezone to format the timestamp. You can get the default timezone with this:

echo date_default_timezone_get();

From what I understand from your question's example, these two timezones must be different, or they would show the same time.

I believe it is standard practice to set all the default timezones to UTC, and convert them to local timezone according to the request's origin. Doing this is not as straight forward as it seems, as timezones are not part of the HTTP spec, but this may help you with that: How to detect user's timezone?.

If you just want to store everything in your local timezone and are confident the users won't mind, you can change the default timezone in php and mysql to your local timezone.

MySQL: How do I set the time zone of MySQL?

PHP: http://php.net/manual/en/function.date-default-timezone-set.php

Community
  • 1
  • 1
squgeim
  • 2,321
  • 1
  • 14
  • 21