Change Date Format - PHP

1

How could I change the format of a date that I receive by POST : 24/01/2017 (dd / mm / yyyy) to this format 2017/01/24 (yyyy / mm / dd)?

$fecha1= $_POST['fecha1'];

The detail is that when I insert it in MySQL I change the format and register it as 2024/01/17 with the value {24/01/2017} .

    
asked by matteo 24.01.2017 в 20:46
source

1 answer

3

You could use something like this:

<?php

$fecha1 = "24/01/2017"; // Obviamente se cambia por $_POST['fecha1'];

$fechaNueva = date('Y/m/d', strtotime(str_replace('/', '-', $fecha1)));

echo $fechaNueva . "\n";

?>

What we do is change slash to scripts , so that it is easier to read towards the strtotime function.

>

This error occurs because of the following (According to the PHP documentation) strtotime () :

  

Note:   The dates in the formats m / d / y or d-m-y are not ambiguous when observing the separator between the different components: if the separator is a slash (/), the North American format m / d / y is assumed; whereas if the separator is a hyphen (-) or a period (.), the European format d-m-y is assumed. If, however, the year is provided in a two-digit format and the separator is a hyphen (-, the date string is analyzed as y-m-d.

Result:

  

2017/01/24

    
answered by 24.01.2017 в 21:13