How to calculate departure time based on input time

Asked

Viewed 67 times

1

I am trying to make a simple point system in PHP only to calculate the time of departure based on the time I log in. For example, my check-in time is 07:00 and check-out is 16:48 what I want to do is set the time I enter and show the time I should leave.

Example: if I enter at 06:30 I entered 30 minutes earlier, then I would have to leave at 16:18, then I would have to decrease 30 minutes of the $output variable, but I can’t find a logic to do this.

HTML

<form  action="dashboard.php?link=home" method="post"  class="ajax_off">
    <div class='row'>  
        <div class="col col-lg-4">
            <div class="form-group mb-3">
            <input type="text" class="form-control" name="hr_entrada" value="07:00:00">
            </div>
        </div>
        <div class="col col-lg-8">
          <div class='row'><button  class="btn btn-icon btn-primary">Calcular</button></div>                    </div>
     </div>
</form>

PHP

$Entrada = (isset($_POST['hr_entrada']) ? $_POST['hr_entrada'] : '07:00:00');
$Saida =  '16:48:00';

Echo 'voce deve sair as '.$Saida;

2 answers

2

To make calculations of hours with php I use the function strtotime();

Each parameter of this function uses the default server time zone.

$duration = array('hours' => 9, 'minutes' => 48);

$entrance =  date("Y-m-d H:i:s");

$leave =  date("Y-m-d H:i:s",  strtotime("+{$duration['hours']} hours {$duration['minutes']} minutes", strtotime($entrance) ) );

echo "Entrada: {$entrance} <br> Saida: {$leave}";
  • Thanks for the reply I managed to resolve

0


Don’t think about the time difference. Think about the interval you need to work on. From your description, it’s 588 minutes. Therefore, the following PHP code can be used:

$horario = new DateTime(isset($_POST['hr_entrada']) ? $_POST['hr_entrada'] : '07:00');
$horario->add(new DateInterval('PT588M'));

echo 'voce deve sair as ' . $horario->format('H:i');

Browser other questions tagged

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