Convierta la marca de tiempo Unix en una fecha legible por humanos usando MySQL


¿Hay una función MySQL que se pueda usar para convertir una marca de tiempo Unix en una fecha legible por humanos? Tengo un campo donde guardo tiempos Unix y ahora quiero agregar otro campo para fechas legibles por humanos.

Author: Teun Zengerink, 2011-06-07

7 answers

Use from_unixtime :

SELECT
  from_unixtime(timestamp) 
FROM 
  your_table
 295
Author: CristiC,
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-06-07 15:28:00

Lo que falta en las otras respuestas (al momento de escribir este artículo) y no es directamente obvio es que from_unixtime también puede tomar un parámetro de formato como:

SELECT
  from_unixtime(timestamp, '%Y %D %M %h:%i:%s')
FROM 
  your_table
 85
Author: User,
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
2013-11-11 00:11:47

Creo que lo que estás buscando es FROM_UNIXTIME()

 31
Author: Ted Hopp,
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-06-07 15:26:13

¿Necesita una marca de tiempo unix en una zona horaria específica?

Aquí hay un trazador de líneas si tiene acceso rápido a la cli de mysql:

mysql> select convert_tz(from_unixtime(1467095851), 'UTC', 'MST') as 'local time';

+---------------------+
| local time          |
+---------------------+
| 2016-06-27 23:37:31 |
+---------------------+

Reemplace 'MST' con la zona horaria deseada. Vivo en Arizona por lo tanto la conversión de UTC a MST.

 14
Author: elbowlobstercowstand,
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-28 07:55:41

¿Por qué molestarse en guardar el campo como legible? Solo nosotros AS

SELECT theTimeStamp, FROM_UNIXTIME(theTimeStamp) AS readableDate FROM theTable WHERE theTable.theField = theValue;

EDITAR: Lo sentimos, almacenamos todo en milisegundos, no en segundos. Solucionarlo.

 5
Author: josh.trow,
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-06-07 15:27:50

Puede utilizar la función DATE_FORMAT. Aquí es una página con ejemplos y los patrones que puede usar para seleccionar diferentes componentes de fecha.

 4
Author: Briguy37,
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-06-07 15:27:51

Manera fácil y sencilla:

select from_unixtime(column_name, '%Y-%m-%d') from table_name

 0
Author: Akash gupta,
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-10-03 10:03:56