El truco aquí es saber qué tipo de columna date_last_active
es. He tenido suficiente experiencia con TIMESTAMP
y DATETIME
saber que uno traduce (y respeta) la variable de sesión MySQL time_zone, el otro no.
mysql> SET SESSION time_zone = 'America/New_York';
Query OK, 0 rows affected (0.00 sec)
mysql> create table timetest ( dt datetime NULL, ts timestamp NOT NULL DEFAULT 0 );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO timetest ( dt, ts ) VALUES ( UTC_TIMESTAMP(), UTC_TIMESTAMP() );
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO timetest ( dt, ts ) VALUES ( NOW(), NOW() );
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM timetest;
+---------------------+---------------------+
| dt | ts |
+---------------------+---------------------+
| 2009-06-27 17:53:51 | 2009-06-27 17:53:51 |
| 2009-06-27 13:53:54 | 2009-06-27 13:53:54 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> set session time_zone='UTC';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM timetest;
+---------------------+---------------------+
| dt | ts |
+---------------------+---------------------+
| 2009-06-27 17:53:51 | 2009-06-27 21:53:51 |
| 2009-06-27 13:53:54 | 2009-06-27 17:53:54 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
Entonces, esto es lo que hago:
- Al insertar en la base de datos, use UTC en PHP y en MySQL. En
my.cnf
Tengo:time_zone=UTC
para evitar cualquier problema en MySQL y en PHP Idate_default_timezone_set('UTC')
. - Usar
UTC_TIMESTAMP()
en lugar deNOW()
- uno es UTC, uno usa la zona horaria local (sesión). Si está siguiendo el primer punto anterior, use UTC_TIMESTAMP(). - Al seleccionar mostrar al usuario,
set time_zone='
zona_horaria_local'
antes de mostrar nada. - Si tiene que mostrar cosas, luego actualice, asegúrese de incluir sus llamadas con los cambios de zona horaria apropiados en PHP y MySQL.
Esperemos que eso sea suficiente para descubrir cómo abordar esto. Hazme saber si tienes más preguntas.