Table used
Consultation held day 31/08 at 21:29hs from the server
SELECT
CONCAT(
TIMESTAMPDIFF(day,NOME_COLUNA,now()) , ' dias ',
MOD( TIMESTAMPDIFF(hour,NOME_COLUNA,now()), 24), ' hs ',
MOD( TIMESTAMPDIFF(minute,NOME_COLUNA,now()), 60), ' min '
) from NOME_TABELA
Upshot
- CONCAT function to join query values
- TIMESTAMPDIFF - function to calculate the difference between dates
Order of consultation
For alphanumeric fields 10 comes before a 2 because the evaluation is done from left to right type by digit.
Table used:
Upshot:
With numeric values the default order is from the lowest to the highest, unless specified DESC
in order by
To order the result, since it is always started by a number, you can convert the result of the query into a number that the sql will capture the part before the letters and convert to a number, then sort the result by this value.
Consultation:
SELECT
CONCAT(
TIMESTAMPDIFF(day,NOME_COLUNA,now()) , ' dias ',
MOD( TIMESTAMPDIFF(hour,NOME_COLUNA,now()), 24), ' hs ',
MOD( TIMESTAMPDIFF(minute,NOME_COLUNA,now()), 60), ' min '
) as resultado from NOME_TABELA ORDER BY CAST(resultado AS UNSIGNED INTEGER)
You already have an answer on the subject: https://answall.com/questions/57/howto calculate a difference%C3%A7a-between-two-dates/70
– novic
The @Bacco response seems like a good suggestion that goes beyond mysql: How to humanize a date in PHP
– Guilherme Nascimento