Format written date "ex. January 12, 2017 "to the standard (Y-m-d)

0

Is it possible to pass this date "January 12, 2017" to the US standard (Y-m-d)?

Any examples?

Thank you.

    
asked by anonymous 15.08.2016 / 00:23

2 answers

3

Using the date_create_from_format function, you can format the date as you wish but first you should make some changes:

$string = "12 de Dezembro de 2017";

$string = preg_replace('/( de ){1,2}/', ' ', $string);

$month = [
        'Janeiro' => 'January',
        'Fevereiro' => 'February',
        'Marco' => 'March',
        'Abril' => 'April',
        'Maio' => 'May',
        'Junho' => 'June',
        'Julho' => 'July',
        'Agosto' => 'August',
        'Novembro' => 'November',
        'Setembro' => 'September',
        'Outubro' => 'October',
        'Dezembro' => 'December'
    ];

$string = str_replace(array_keys($month), array_values($month), $string);

$dr= date_create_from_format('d M Y', $string);
echo $dr->format('Y-m-d');
    
15.08.2016 / 01:32
1
date('Y-m-d', strtodate($string));
    
28.11.2016 / 17:10