How to calculate the time difference between certain hours?

Asked

Viewed 1,699 times

1

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

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

With this I take the time that is recorded on the bench with type of field timestamp without time zone, but need to know what is the time difference with the current time that in the case would:

date('Y-m-d H:i:s')    
  • 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

  • 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

  • It is perfect to create a date with a timestamp coming from the database, which will make the problem a normal difference of php dates

  • 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 ?

  • 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;

  • That’s more or less it

Show 1 more comment

1 answer

2


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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.