compare day between dates

Asked

Viewed 44 times

0

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

follows code.

</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";
?>">

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;

0

Using jQuery, you could do the following (I didn’t test the code directly, it’s just an idea typed directly into 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

Thus, every time you leave the fields containing such a class, the result will be calculated.

Browser other questions tagged

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