2

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?

Rick H.
  • 23
  • 3
  • I understand you use php date and format it for creating this string. Can you check the given date with this date (before it is formatting like string) instead that retrieve the date from string? – Stefino76 Oct 08 '22 at 17:02
  • Can you try with the answer https://stackoverflow.com/a/72147814/1213708 – Nigel Ren Oct 08 '22 at 17:39

1 Answers1

2

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)
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • Works great! Just one thing: its 2 hours late since its currently summertime in germany. So 6 October - 1:59 would say its 5 October. Can i somehow easily check this? Also with wintertime which is 1 hour late? Thanks! – Rick H. Oct 09 '22 at 19:35
  • You could use a timezone. `$timezone = new DateTimeZone('UTC'); $formatter = new IntlDateFormatter('de-DE', IntlDateFormatter::FULL, IntlDateFormatter::FULL, $timezone, IntlDateFormatter::GREGORIAN, 'EEEE - dd. MMMM yyyy - HH:mm');` – Markus Zeller Oct 09 '22 at 19:57