3

I'm from India. Hosting my site in 000webhost. I'm tracking the IPs visiting and their times. but the time is wrong i.e I think times are according to the time zone of 000webhost server. How to convert them according to Indian timezone.

this is the code I am using:

date_default_timezone_set('Asia/Kolkata');
$query="INSERT INTO visitor_ip ";
$query.="VALUES('{$_SERVER["REMOTE_ADDR"]}',now())";
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jayadratha Mondal
  • 759
  • 1
  • 10
  • 21
  • 1
    Also the [unix timestamp](http://en.wikipedia.org/wiki/Unix_time) is timezone independent so all of your conversions could take place at run time and you can use this as the backbone. – rfoo Sep 15 '13 at 07:30

1 Answers1

5

One way to solve it is running an SQL query first which sets the MySQL connection time zone: SET time_zone = 'Asia/Kolkata';. More info here:

Another way to solve it is using PHP date and time functions (outside quotes) instead of NOW() (inside quotes) to query the time as a string, and passing that string to MySQL. date_default_timezone_set will affect the PHP date and time functions.

Community
  • 1
  • 1
pts
  • 80,836
  • 20
  • 110
  • 183
  • I'm using date_default_timezone_set('Asia/Kolkata'); In php.net it says after this function all function related to time date will use this time zone. Then why its not happening – Jayadratha Mondal Sep 15 '13 at 07:30
  • 1
    @Jayadratha Mondal: `date_default_timezone_set` affects only the PHP time zone, and `NOW()` uses the MySQL time zone. That's why it doesn't work the way you tried it. – pts Sep 15 '13 at 07:32
  • 1
    Thanks for informing this. So Can I use this?? date_default_timezone_set('Asia/Kolkata'); $time=date("Y-m-d / h-i-s A",time()); $query="INSERT INTO visitor_ip "; $query.="VALUES('{$_SERVER["REMOTE_ADDR"]}',$time)"; – Jayadratha Mondal Sep 15 '13 at 07:56