Decrease php hours [duplicate]

0

I'm having trouble handling the time to lower it in php.

My idea is to get the time that comes from the database and decrease with the current time and bring the rest, but I'm not getting it.

code I am using as an example:

$results =DB::select("SELECT HoraCriacao FROM partidas WHERE (Aceitou=2) ORDER by Id desc LIMIT 1 ");
                foreach($results as $row){
                    $Hroin=$row->HoraCriacao;
                }
                $Hroin =  strtotime($Hroin);
                $Hroin=date('H:i:s',$Hroin);
                $Hroin=date_create($Hroin);
                $Horat= date('H:i:s');
                $Horat = explode(":", $Horat);
                date_sub($Hroin, date_interval_create_from_date_string($Horat[0].' hours'));
                date_sub($Hroin, date_interval_create_from_date_string($Horat[1].' minute'));
                date_sub($Hroin, date_interval_create_from_date_string($Horat[2].' second'));
                $Hroin=date_format($Hroin, 'H:i:s');
                $Hroin = explode(":", $Hroin);
                $banco=[
                    "mod" => "3",
                    "cron" => "$Hroin[0]:$Hroin[1]:$Hroin[2]"
                ];
                return response()->json($banco);
    
asked by anonymous 20.08.2018 / 19:34

1 answer

-2

I could not quite understand what you want, but if you need to calculate the difference between dates, you can use the DateTime class of PHP .

<?php

$data_one = new DateTime( '2018-06-11 12:00:00' ); // Pode ser a data que vem do seu banco
$data_two = new DateTime(); // Se você não passar nenhum parâmetro, ele pega a data atual
$interval = $data_one->diff( $data_two ); // Calcula a diferença entre as duas datas

echo '<pre>';
print_r( $interval );
die;

# Retorno do print_r
DateInterval Object
(
    [y] => 0
    [m] => 2
    [d] => 8
    [h] => 21
    [i] => 50
    [s] => 20
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 69
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

With this you have the difference between the dates of year, month, day, hour, minute and second.

    
20.08.2018 / 19:48