Change the format of the date I receive from YYYY-MM-DD to DD / MM / YYYY [duplicated]

3

I am a rookie, I have the following date: 2010-04-19 .
I would like to convert this date to the format DD-MM-YYYY

Example, my code.

// "fecha" => $this->input->post(date("d/m/Y", strtotime('fecha'));

$usuario = array(
    "nombre" => $this->input->post('nombre'),
    "fecha" => $this->input->post('fecha') /YYYY-MM-DD
);
    
asked by Diego Sagredo 08.03.2017 в 17:01
source

7 answers

7

To make the change you could use strtotime() and date() for example:

$originalDate = "2017-03-08";
$newDate = date("d/m/Y", strtotime($originalDate));

You will get in $newDate 08/03/2017

If you want to put that date in an arrangement you can do it in the following way:

$usuario = array(
                "nombre" => $this->input->post('nombre'),
                "fecha" => $newDate
           );

Since you have stored the date in variable $newDate

    
answered by 08.03.2017 в 17:09
2

Before passing the elements of the form by Post in your js, declare this code:

  $("#fecha" ).datepicker( "option", "dateFormat", "dd-mm-yy" ); 

this will change the format of your date from YYYY-MM-DD to DD / MM / YYYY

    
answered by 08.03.2017 в 17:05
1
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');

more info on the official php website link

    
answered by 08.03.2017 в 17:06
1

If the date you are sending them from datepicker would be the following

$(document).ready(function () {                    
    $('#fecha').datepicker({
        format: "dd/mm/yyyy",
        clearBtn: true,
        language: "es",
        autoclose: true,
        keyboardNavigation: false,
        todayHighlight: true
    });              
});
    
answered by 08.03.2017 в 17:13
0

You can receive it in the php file and format it like this

$Nueva = date('d-m-Y', strtotime($_POST['fecha']));
    
answered by 08.03.2017 в 18:58
0

Another way would be to use the class DateTime with DateTime::format

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

echo $fecha_d_m_y; // 01/02/2017

See example

    
answered by 08.03.2017 в 22:04
0
$newDate = date("Y-m-d", strtotime($data["fechai"]));

This was the one that helped me to go from d-m-Y to Y-m-d and thus insert in field Date in SQL.

    
answered by 16.01.2018 в 17:36