Data Datepicker - Processing

Asked

Viewed 644 times

3

Sirs:

The date I have on datepicker is in format dd/mm/yy, however, the date in the database is yy-mm-dd (date field). How do I make datepicker to display the default BR date and when it comes via POST, enter as EN format?

3 answers

1

I usually treat this data on the server side, and using PHP I do so:

<input type="text" name="minha_data" 
 id="minha_data" class="form-control data datepicker" 
 value="<?php data_us_to_br($geral->dt_atendimento); ?>"    
 placeholder="dd/mm/aaaa" maxlength="10" minlength="10" />

and on the server side, when I will receive the POST like this:

$valorMinhaData = data_br_to_us($_POST['dt_verificacao']);

and the functions I’m using in the case are these :

function data_us_to_br($dateUSA)
{
    if($dateUSA) {
         $ano = substr($dateUSA, 0, 4);
         $mes = substr($dateUSA, 5, 2);
         $dia = substr($dateUSA, 8, 2);
         $dateBR = $dia .'/'. $mes .'/'. $ano;

         return $dateBR;
    } 

    return NULL;
}


function data_br_to_us($dateBR)
{
    if($dateBR) {
         $ano = substr($dateBR, 6, 4);
         $mes = substr($dateBR, 3, 2);
         $dia = substr($dateBR, 0, 2);
         $dateUSA = $ano .'-'. $mes .'-'. $dia;

         return $dateUSA;
    } 

    return NULL;
}

1


If you want to deal with sending:

var dataQuebrada = $("#datepicker").val().split("/");
var novaData = new Date("20" + dataQuebrada[2], dataQuebrada[1] - 1, dataQuebrada[0]); 
// new Date(year, month, day);
// -1 no mês porque ele começa do 0
// "20", pois você só tem os 2 últimos digitos

If you want to deal with receiving:

var $datepicker = $("#datepicker");
var dataQuebrada = $("#datepicker").val().split("-");
var novaData = new Date("20" + dataQuebrada[0], dataQuebrada[1] - 1, dataQuebrada[2]); 
$datepicker.datepicker();
$datepicker.datepicker('setDate', novaData);
  • I don’t understand... The Post comes in php.

  • @Andrébaill I updated the answer

0

Da p/ do so with a single command line:

var d = $.datepicker.formatDate( "yy-mm-dd", $('#calendario').datepicker('getDate') );

Where the variable d is in mysql format

And the $('#calendario'). datepicker('getDate') Take whatever format is

https://api.jqueryui.com/datepicker/

  • But how do I get it back in php? Grateful

  • in this case you can use javascript to fill in some field of your form and send by Submit, or make an ajax, whatever it is

Browser other questions tagged

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