Change the input date format

0

I would like to know how to change the format of a date type input

I have this input showing dd-mm-yyyy

<p>Fecha <input type="date" name="fecha" required></p>

But at the time of registering with php in my database (the column where you register is in text format) appears in yyyy-mm-dd format. p>

Does anyone know why?

    
asked by Tefef 22.03.2018 в 16:22
source

6 answers

1

If the front has the format as you need it, simply change that format in the back to save it as you wish in your database.

Assuming that your field is varchar as you said.

$fecha = new DateTime('2006-12-30');
echo $fecha->format('d-m-Y');

Otherwise it could be like this:

$fecha = new DateTime($_POST['fecha']);
echo $fecha->format('d-m-Y');
    
answered by 22.03.2018 в 16:48
0

The format of inputs of type date is rendered by the browser:

  

The control's UI varies in general from browser to browser; at the   moment support is patchy, see Browser compatibility for further   details In unsupported browsers, the control degrades gracefully to a   simple.

Source

More info

Therefore, you can not change the format directly if your input is still of type date . If it's not much of a problem, it's best to declare it as type text and use Javascript to format it (with a jQuery datepicker or Bootstrap, for example)

    
answered by 22.03.2018 в 16:29
0

The problem is that in the databases if you use the type date or datetime you will save yyyy-mm-dd if you want to obtain it dd-mm- yyyy you must save it in a field varchar

    
answered by 22.03.2018 в 16:31
0

I see that in the tags you are using php, you can leave the html equal but in php you use the function date:
date('d-m-Y',strtotime(/*nombre de tu campo fecha*/$fecha ));

    
answered by 22.03.2018 в 16:39
0

You can not change the format of input[date] , not at least until today's date. What you can do is give it the format you want before saving the date.

You can achieve this by using momentjs , convert it to a momentjs object and then give it the desired format:

function obtenerFecha(e)
{

  var fecha = moment(e.value);
  console.log("Fecha original:" + e.value);
  console.log("Fecha formateada es: " + fecha.format("DD/MM/YYYY"));
}
<script src="https://momentjs.com/downloads/moment.js"></script>

<input type="date" id="date" onchange="obtenerFecha(this)" />
    
answered by 22.03.2018 в 16:40
-2

You must define the pattern you require:

 <input type="date"  pattern="[0-9]{2}-[0-9]{2}-[0-9]{4}" required/>
    
answered by 22.03.2018 в 16:39