compare day between dates

0

I have a form where I compare two dates ( dt_ocorrencia e dt_prev_entrega ) and I bring the result, so everything is ok. But I would like to type in one of the fields to update the result.

code follows.

</div>
          </div>
           <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Dt Ocorrência:</label>
            <div class="col-sm-10">
<input type="date" required name="dt_ocorrencia" class="form-control" value="<?php echo $data_inicial = date('Y-m-d'); ?>">

            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Prev. de Entrega:</label>
            <div class="col-sm-10">
<input type="date" required name="dt_prev_entrega" class="form-control" value="<?php echo $data_final = date('2016-08-12'); ?>">
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">SLA:</label>
            <div class="col-sm-10">
            <input type="text" required  name="sla" Onclick="calc()" class="form-control" value="<?php


// Calcula a diferença em segundos entre as datas
$diferenca = strtotime($data_final) - strtotime($data_inicial);
//Calcula a diferença em dias
$dias = floor($diferenca / (60 * 60 * 24));

echo "$dias";
?>">
    
asked by anonymous 12.08.2016 / 19:37

2 answers

3
/*  data no formato Y/m/d */
$dataInicial = strtotime("2017-01-01");
$dataFinal = strtotime("2017-02-01");
$diff = $dataFinal - $dataInicial;
echo floor($diff / (60 * 60 * 24));

If you use PHP 5.3 >

$date1 = new DateTime("2017-01-06");
$date2 = new DateTime("2017-02-09");
$diff = $date2->diff($date1)->format("%a");
echo $diff;
    
13.07.2017 / 13:20
0

Using jQuery, you could do the following (I did not test the code directly, it's just an idea typed directly in this editor):

1) Give a class name to its two input elements, and two 'id' attributes, such as

<input type="date" id="idDatPrimeiraData" required name="dt_ocorrencia" class="form-control claDatDatas" value="<?php echo $data_inicial = date('Y-m-d'); ?>">

and

<input type="date" id="idDatSegundaData" required name="dt_prev_entrega" class="form-control claDatDatas" value="<?php echo $data_final = date('2016-08-12'); ?>">

Then, make a JQuery code:

jQuery(document).ready(function (){
    jQuery( ".claDatDatas" ).blur(function() {
        var primeiraData=jQuery("#idDatPrimeiraData").val();
        var segundaData=jQuery("#idDatSegundaData").val();
            jQuery.get("suaPagina.php",{primeira:primeiraData,segunda:segundaData},function(retorno){
                //em 'suaPagina.php' capture os valores $_GET{'primeira'] e $_GET['segunda'] e mande retornar o cálculo, conforme você demonstrou na sua explicação
                alert ("A diferença entre as datas é "+retorno);
            });//get
        });//blur
});//ready

So, every time you exit the fields that contain such a class, the result will be calculated.

    
13.07.2017 / 12:26