How to change the date format received by $ _POST?

0

I am developing a small application with PHP and HTML , and I see myself in need of changing the format of <input type="date"/> since it is the English format and I need it in Spanish.

I have no idea how to do it, I've read that using date_format($fechaNacimiento, 'd/m/y') that function or also using the date function like this:

 
$fecha = new DateTime('2017-02-01');
$fecha_m_d_y = $fecha->format('d/m/Y');

echo $fecha_m_d_y;

But neither of them I have been able to apply them, because I need to apply it to $_POST and for these functions they are string , and I do not know how to pass the $_POST to string . Attachment code

HTML and PHP

              <tr>
                <td>Fecha Nacimiento:</td>
                <td><input type="text" name="jug_fec_nac" id="jug_fec_nac" value="<?php echo date_format($fechaNacimiento, 'd/m/y');
                ?>"/></td>
              </tr>

Definition of variables

$fechaNacimiento = $_POST['jug_fec_nac'];

That returns the date with English format ( 2017/05/31 ) and I need it to receive it by the $_POST change it to ( 31/05/2017 )

    
asked by scorpions 06.01.2018 в 21:05
source

2 answers

1

You can do it in the following ways:

Solution 1

Using DateTime::createFromFormat :

<?php
// Fecha en formato yyyy/mm/dd
$fecha = DateTime::createFromFormat('Y/m/d', $_POST['jug_fec_nac']);
// Fecha en formato dd/mm/yyyy
$fechaNacimiento = $fecha->format('d/m/Y');

Solution 2:

Using strtotime and strftime

<?php
// Como la fecha viene en formato ingles, establecemos el localismo.
setlocale(LC_ALL, 'en_US');

// Fecha en formato yyyy/mm/dd
$timestamp = strtotime($_POST['jug_fec_nac']);

// Fecha en formato dd/mm/yyyy
$fechaNacimiento = strftime("%d/%m/%Y", $timestamp);
    
answered by 06.01.2018 / 22:30
source
0

You can use this form to modify the date format link

But I advise you to change the type of your form instead of putting "text" try to put "date" possibly so it will be better to apply the POST method link

    
answered by 06.01.2018 в 21:54