0

Right now my query looks like this (relevant part):

UPDATE last_seen=NOW()

But it inserts a time which is 2 hours earlier than mine. Is there any way to adjust it?

Thanks

Johan
  • 35,120
  • 54
  • 178
  • 293

4 Answers4

2

Set the timezone on your DB.. take a look this post. https://stackoverflow.com/a/3451971/1227435

This is another great resource:

Having Timezone problems with PHP and MySQL

Community
  • 1
  • 1
Roberto Navarro
  • 948
  • 4
  • 16
1

I recommend you to try something like so:

Have an hidden input to collect user's timezone

<input type="hidden" name="offset" class="offset">
$('.offset').val(new Date().getTimezoneOffset());

Then get it before the update and convert it to seconds like so

$offset    = intval($_REQUEST['_offset']) * 60;

Finally add it to time():

$now = time() + $offset;

Doing the above, you have a variable with the client's time in his timezone. If you don't want that, and want a common time for everybody just set your time zone with this in your index.php:

date_default_timezone_set('TIMEZONE IN HERE'); //Supported timezone list: http://www.php.net/manual/en/timezones.php

But it's better if you set it in your php.ini (the timezone) or changing server's timezone (Debian/Ubuntu: dpkg-reconfigure tzdata)

Alex
  • 7,538
  • 23
  • 84
  • 152
0

Sounds like MySQL is not on the same timezone as you.

You have some options:

  • Set the timezone for MySQL.
  • Use PHP to set the date before storing.
  • Store as a UTC timestamp and then display correct local time. More code, but I recommend.
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

http://dev.mysql.com/doc/refman//5.5/en/time-zone-support.html you need to set MySQL's notion` of timezone. Hope that helps...

hd1
  • 33,938
  • 5
  • 80
  • 91