How to calculate the time difference between certain hours? [duplicate]

1
public function checks($request)
{
    $token = $this->count->All();

    foreach($token as $tokens){
      $tokens->system_contador_user_update_at

With this I get the time it is written to the bank with type of field timestamp without time zone , but I need to know what is the difference time with the current time that would be: / p>

date('Y-m-d H:i:s')    
    
asked by anonymous 01.08.2017 / 14:29

1 answer

2

In PHP, the diff() method of the DateTime class 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) );

Output:

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;
    
01.08.2017 / 15:25