phpdatedatetimestrtotime

Convert german date string into date and check if its before today


I have a string with my german date Freitag - 09. September 2022 - 14:00 (Format is: l - d. F Y - H:i). It need to be displayed in this format on my homepage thats why its in this format.

Now i want to parse this string into a date that i can check if the given date is before todays date (i only want to check day, month and year, so time is unnecessary).

I already tried a lot of things with strtotime and also DateTime::createFromFormat but nothing worked for me.

Is there any solution for that?


Solution

  • You can use the IntlDateFormatter to parse the German date. This requires the intl PHP extension.

    $germanDateText  = 'Freitag - 09. September 2022 - 14:00';
    $formatter       = new IntlDateFormatter('de-DE', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Berlin', IntlDateFormatter::GREGORIAN, 'EEEE - dd. MMMM yyyy - HH:mm');
    $germanTimestamp = $formatter->parse($germanDateText);
    $germanDayStart  = strtotime(date('Y-m-d', $germanTimestamp));
    $todayDayStart   = strtotime(date('Y-m-d'));
    
    $isBeforeToday   = $germanDayStart < $todayDayStart;
    var_dump($isBeforeToday);
    

    Output

    bool(true)