How can I format a String to date with PHP

0

Greetings as they are all

I have this date with this format, it is a string Wed Sep 12 2018 14:47:35 GMT-0500 (Colombian standard time) and I want to cover it in this format 2018-09 -12 14:47:35 Someone knows how I can solve my problem.

Thank you in advance to everyone.

    
asked by Jeffry Jose Barrios Gomez 12.09.2018 в 22:19
source

2 answers

1

You can create an instance of the class DateTime by passing it your date in the original format you have and then call the function format to give it the output format you want. Here is the example:

//Fecha en el formato original
$fecha = 'Wed Sep 12 2018 14:47:35 GMT-0500';
//Instancia de DateTime con la fecha original
$date = new DateTime($fecha);
//Muestro el texto con la fecha en el formato esperado
echo $date->format('Y-m-d H:i:s');

Or you can do it in this other way

//Fecha original
$fecha = 'Wed Sep 12 2018 14:47:35 GMT-0500';
//Establezco la zona horaria a Colombia para que me devuelva bien la hora
date_default_timezone_set('America/Bogota');
//Muestro la feha en el formato indicado
echo date('H:i:s', strtotime($fecha));

I hope it has helped you

    
answered by 12.09.2018 / 22:59
source
0

A few days ago I had the same problem and searching the Internet I found this and it worked perfectly:

date_default_timezone_set('America/Bogota');

setlocale(LC_TIME, 'spanish');

echo strftime("%d-%m-%y %H:%M:%S")

try and tell me

If you have any questions or modifications to the format, you will investigate the strftime() function in The php documentation

    
answered by 12.09.2018 в 23:18