Treat date in php "2017-09-02T00: 00: 00.000Z",

2

Very good, I receive this date form in a Json: 2017-09-02T00:00:00.000Z and I would like to be able to treat it to be dd/MM/YYYY . Does the function exist in php? Thanks

    
asked by Lorenzo Martín 21.10.2017 в 16:49
source

2 answers

3

PHP has the function date_format for it, which is an alias of DateTime :: format

As for the format dd/MM/YYYY , in PHP it would read like this: "d/m/Y" , where :

  • d : Day of the month, 2 digits with leading zeros

  • m : Numerical representation of a month, with leading zeros

  • Y : A full numerical representation of a year, 4 digits

Code example:

$str='2017-09-02T00:00:00.000Z';
$formato = 'd/m/Y'; 
$fecha=date_format(date_create($str), $formato);
echo $fecha;

Or ... if you want it in one line:

echo date_format(date_create('2017-09-02T00:00:00.000Z'), 'd/m/Y');

Result:

02/09/2017

Or ... if you like Object Oriented Programming (OOP), you create a date object from your string using the class DateTime :

$str='2017-09-02T00:00:00.000Z';
$formato = 'd/m/Y'; 
$oFecha = new DateTime($str); //Aquí se crea un objeto fecha a partir de la cadena
$fecha= $oFecha->format($formato); //Aquí extraes la fecha según el formato y puedes seguir usando $oFecha para otras cosas si lo necesitas
echo $fecha;
    
answered by 21.10.2017 / 17:13
source
2

You can use strtotime and date for what you want.

strtotime converts to unix date

date gives you the format you want.

echo date('d/m/y',strtotime('2017-09-02T00:00:00.000Z'));
    
answered by 21.10.2017 в 16:58