Date subtraction today - initial date in PHP and bring result eg: 1 day 5hs and 25min [duplicate]

0

I have 2 variables in PHP. The first is the $ date request date and the second is the now $ date now. I need to list how long the request is open.

The $ data requested is queried in MYSQL.

Both are in the same format: ('Y-m-d H: i').

I would like, in php, to make a $ account now - $ date request and have a result, for example, 1 day, 5hs and 25min, if possible, already do this in the mysql query so I can sort by what is the more open time.

    
asked by anonymous 30.08.2017 / 23:51

1 answer

1

Table used

Queryperformedon08/31at9:29pm

SELECTCONCAT(TIMESTAMPDIFF(day,NOME_COLUNA,now()),'dias',MOD(TIMESTAMPDIFF(hour,NOME_COLUNA,now()),24),'hs',MOD(TIMESTAMPDIFF(minute,NOME_COLUNA,now()),60),'min')fromNOME_TABELA

Result

  • CONCATfunctiontomergequeryvalues
  • TIMESTAMPDIFF-functiontocalculatethedifferencebetweendates

Queryordering

  

Foralphanumericfields10comesbeforethe2becausetheevaluationisdonefromlefttorightdigittodigit.

Tableused:

Result:

Withnumericvalues,thedefaultorderingisfromlowesttohighest,unlessspecifiedasDESCinorderby

Tosorttheresult,sinceitisalwaysstartedbyanumber,youcanconverttheresultofthequeryintoanumberthatsqlwillcapturethepartbeforethelettersandconverttoanumber,theresultbythisvalue.

Inquiry:

SELECTCONCAT(TIMESTAMPDIFF(day,NOME_COLUNA,now()),'dias',MOD(TIMESTAMPDIFF(hour,NOME_COLUNA,now()),24),'hs',MOD(TIMESTAMPDIFF(minute,NOME_COLUNA,now()),60),'min')asresultadofromNOME_TABELAORDERBYCAST(resultadoASUNSIGNEDINTEGER)

    
31.08.2017 / 04:26