Cómo convertir timestamp a datetime en MySQL?


Cómo convertir 1300464000 a 2011-03-18 16:00:00 en MySQL?

 89
Author: compile-fan, 2011-03-19

5 answers

Utilice el FROM_UNIXTIME() función en MySQL

Recuerde que si está utilizando un framework que lo almacena en milisegundos (por ejemplo, la marca de tiempo de Java) tiene que dividir por 1000 para obtener la hora Unix correcta en segundos.

 152
Author: Richard Tuin,
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-10-18 14:26:48
DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%d-%m-%Y') as "Date" FROM `orders`

Esta es la solución definitiva, si la fecha está en formato codificado como 1300464000

 46
Author: Kingshuk Deb,
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
2014-09-08 13:23:46

Para responder el comentario de Janus Troelsen

Use UNIX_TIMESTAMP en lugar de TIMESTAMP

SELECT from_unixtime( UNIX_TIMESTAMP(  "2011-12-01 22:01:23.048" ) )

La función TIMESTAMP devuelve una Fecha o una Fecha y no una marca de tiempo, mientras que UNIX_TIMESTAMP devuelve una marca de tiempo unix

 5
Author: ksvendsen,
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
2014-02-21 17:32:40

SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
Este trabajo para mí

 2
Author: Leo,
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-08-14 11:06:46

Puede usar

select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;

Para una descripción detallada acerca de

  1. from_unixtime()
  2. unix_timestamp()
 0
Author: user2613580,
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-05-24 07:44:55