Decrease php hours

Asked

Viewed 165 times

0

I’m having trouble fixing the time to decrease it in php.

My idea take the time that comes from the database and decrease with the current time and bring the rest , however.

code I’m 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);
  • Friend please try to explain better, because your code is very confused and I could not understand your need.

  • In the Database there is an event time when it was created example : $horain="14:40:00" ; then I want to decrease with the current time $horaat="14:50:00"; then I want to catch the difference

  • Are you wearing Laravel? That one DB::select is familiar

  • @Kayobruno, then he’d have to go back to the rest like 00:10:00

  • @Wallacemaxters, yes I know very little of the Aravel

  • Missed to tag then, huh

  • I put down an answer on how to calculate the difference between dates. See if it helps you with something.

  • Dude, I always try to use https://carbon.nesbot.com/docs/ for date manipulation in Laravel

Show 3 more comments

1 answer

-2


I couldn’t understand very well what you want, but if you need to calculate the difference between dates, you can use the class Datetime 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.

Browser other questions tagged

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