0

I'm trying to parse a german string to a date that I can reformat for a sql database.

The string I've got looks like: 15. Mai 2015, which is basically this format: d. F Y.
How can I reformat it to a date in this format: Y-m-d using PHP? I've tried strtotime() with the correct locale but it wasn't successful.


Edit 1:

I've tried this:

setlocale(LC_TIME, "de_DE");
date_format(
    date_create_from_format("d. F Y", $str),
    'Y-m-d'
);

and this:

setlocale(LC_TIME, "de_DE");
strftime("%Y-%m-%d", strtotime($str))


Edit 2:

I've solved it like suggested:

function parseDate($str) {
    $months = array(
        "Januar"    => "January",
        "Februar"   => "February",
        "März"      => "March",
        "April"     => "April",
        "Mai"       => "May",
        "Juni"      => "June",
        "August"    => "August",
        "September" => "September",
        "Oktober"   => "October",
        "November"  => "November",
        "Dezember"  => "December"
    );

    return strtotime(strtr($str, $months));
}

But isn't there a more elegant way?

tobs
  • 699
  • 2
  • 8
  • 24

1 Answers1

1

With reference to Converting a German date to Y-m-d does not work?

You need to replace string for month by english month name in order to use strtotime.

Community
  • 1
  • 1