Consulta MySQL para la hora GMT actual


Esto suena bastante simple, pero no he sido capaz de averiguar cómo usar una simple instrucción SELECT para devolver la hora actual en GMT.

He estado tratando de usar CONVERT_TZ() para convertir NOW() a GMT basado en la zona horaria del servidor y la zona horaria GMT, pero por alguna razón devuelve NULL cuando pongo las zonas horarias de texto. La única manera de obtener un resultado es realmente poner en las compensaciones que se está volviendo demasiado complicado para lo que debería ser una operación realmente simple. Esto es lo que quiero decir:

mysql> SELECT CONVERT_TZ(NOW(),@@global.system_time_zone,'GMT');
NULL

mysql> SELECT CONVERT_TZ(NOW(),'PST','GMT');
NULL

mysql> SELECT CONVERT_TZ(NOW(),'-08:00','+00:00');
2010-02-13 18:28:22

Todo lo que necesito es una simple consulta para devolver la hora actual en GMT. Gracias de antemano por su ayuda!

Author: fedorqui, 2010-02-13

10 answers

Solo use UTC (no se ve afectado por el horario de verano)

SELECT UTC_TIMESTAMP();

Contenido antiguo para referencia:

Esto debería funcionar, pero con

SELECT CONVERT_TZ(NOW(),'PST','GMT');

También obtuve NULL como resultado. bastante divertido el ejemplo en el docu de mysql también devuelve null

SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');

Http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_convert-tz parece que ha encontrado un error en mysql. (gracias a +Stephen Pritchard)

Usted podría try:

SET @OLD_TIME_ZONE=@@TIME_ZONE;
SET TIME_ZONE='+00:00';
SELECT NOW();
SET TIME_ZONE=@OLD_TIME_ZONE;

Ok no es exactamente lo que quería (sus 4 consultas, pero solo una selección: -)

 34
Author: Rufinus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-13 16:20:56

NO hay ERROR en CONVERT_TZ()

Para usar CONVERT_TZ() necesita instalar las tablas de zona horaria de lo contrario MySQL devuelve NULL.

Desde la CLI ejecute lo siguiente como root

# mysql_tzinfo_to_sql/usr/share | zoneinfo / mysql-u root mysql

VÉASE http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

Gracias

Http://www.ArcanaVision.com (SJP 2011-08-18)

 13
Author: Stephen Pritchard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-18 16:31:27

Nota: GMT podría tener DST UTC no tiene DST

SELECT UTC_TIMESTAMP();

He hecho un cheatsheet aquí: ¿Debería MySQL tener su zona horaria establecida en UTC?

 11
Author: Timo Huovinen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:25:02

La forma segura de obtener la fecha y hora UTC actual es:

SELECT CONVERT_TZ(NOW(), @@session.time_zone, '+0:00')
 4
Author: FrankerZ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-24 20:53:58

Después de ver todas las respuestas anteriores y ver que no es fiable para convertir a PST, acabo de utilizar esto:

DATE_SUB (user_last_login, INTERVALO 7 horas)

 0
Author: Mauvis Ledford,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-16 23:33:57

Esto debería funcionar - creo que se ha especificado mal su zona horaria. Usando Chicago como ejemplo

SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT')
 0
Author: konung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-13 22:50:49

Cuando la marca de tiempo del servidor de origen es desconocida, pero el requisito es para una tz específica, haga lo siguiente:

select convert_tz(utc_timestamp(),'+05:00','+00:00')

Esto usa CST como ejemplo. utc_timestamp garantizará un resultado '+00: 00 ' sin importar dónde lo consultes. El' +05:00 ' es el desplazamiento deseado. Esto funciona muy bien al golpear varios servidores desconocidos y le asegura que devolver el resultado será todo en un conjunto de resultados tz común.

 0
Author: Mark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-15 16:09:38

Estaba buscando mi zona horaria que es GMT + 6 (Zona horaria de Asia/Dhaka)

El servidor MySQL está en NOSOTROS. El de abajo funcionó para mí.

SELECCIONE CONVERT_TZ (UTC_TIMESTAMP( ) , '+06:00', '+00:00' )

 0
Author: Zakaria Bin Abdur Rouf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-26 08:22:36

Esto debería funcionar SELECT date_add (UTC_TIMESTAMP (), interval 330 minute);

 0
Author: Anand Vishwakarma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-17 06:13:55

No hay ningún error en la función CONVERT_TZ. Obtienes NULL porque usas nombres/abreviaturas de zonas horarias. Mysql no sabe lo que significa este 'GMT','PST', etc. a menos que instale mysql.time_zone_name tabla. Sin embargo, si usas números, siempre funcionará.

mysql> SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT');

Devuelve NULL

mysql> SELECT CONVERT_TZ(NOW(),'-08:00','+00:00');

Devuelve 2016-06-24 17:58: 33

 -1
Author: Daniel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-24 14:13:04