In PHP, the method diff()
class DateTime
is able to calculate the difference between two dates:
$agora = new DateTime();
$data = new DateTime('2020-01-25 12:21:33');
var_dump( $agora->diff($data) );
Exit:
object( DateInterval )#3 (15)
{
["y"]=> int(2)
["m"]=> int(5)
["d"]=> int(24)
["h"]=> int(2)
["i"]=> int(45)
["s"]=> int(0)
["weekday"]=> int(0)
["weekday_behavior"]=> int(0)
["first_last_day_of"]=> int(0)
["invert"]=> int(0)
["days"]=> int(907)
["special_type"]=> int(0)
["special_amount"]=> int(0)
["have_weekday_relative"]=> int(0)
["have_special_relative"]=> int(0)
}
In Postgres:
-- Diferenca total em Segundos
SELECT extract(epoch from ('2020-01-25 12:21:33' - now()));
-- Diferenca por partes:
SELECT
date_part( 'year', age('2020-01-25 12:21:33',now()) ) AS qtd_anos,
date_part( 'month', age('2020-01-25 12:21:33',now()) ) AS qtd_meses,
date_part( 'day', age('2020-01-25 12:21:33',now()) ) AS qtd_dias,
date_part( 'hour', age('2020-01-25 12:21:33',now()) ) AS qtd_horas,
date_part( 'minute', age('2020-01-25 12:21:33',now()) ) AS qtd_minutos,
date_part( 'second', age('2020-01-25 12:21:33',now()) ) AS qtd_segundos;
no face doesn’t have much to see here I need to take the bank time and take the current time take the difference in minutes and then make the validations
– Pedro Amorim
I’m not using the date class I’m almost sure it’s something with the microtime() function but I don’t know very well how to manipulate it
– Pedro Amorim
It is perfect to create a
date
with atimestamp
coming from the database, which will make the problem a normal difference of php dates– Isac
then more expensive even so I will not get the difference between hours minutes and tals so that it can be validated if more than 1 hour generate new token, if not token still valid .... this are just simple and tals that I already have just to needing to pick up that difference even in minutes hours and such ?
– Pedro Amorim
Living How to calculate the difference between two dates?, you want to calculate two date ranges so it’s not ?
SELECT '2017-08-01 09:30:00'::TIMESTAMP - TO_CHAR(now(), 'yyyy-mm-dd HH:I:S')::TIMESTAMP;
– Marcos Henzel
That’s more or less it
– Pedro Amorim