1

I have the following snippet in my code that prints "06. Oktober 2016"

<?php
    // german umlauts in date not possible without date formatter
    $fmt = new IntlDateFormatter('de_DE' ,IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Berlin', IntlDateFormatter::GREGORIAN);
    $fmt->setPattern("MMMM");
    $event_date_format = DateTime::createFromFormat('Ymd', $event_date[0]);
    $dateOp = $event_date_format->format('d') . ". " . $fmt->format($event_date_format) . " " . $event_date_format->format('Y');
?>

I want however to also include "Montag, 06. Oktober 2016, 12:33 Uhr"

I can't seem to make that work when using this for instance:

$dateOp = $event_date_format->format('l') . ", " . $event_date_format->format('d') . ". " . $fmt->format($event_date_format) . " " . $event_date_format->format('Y') . ", " . $event_date_format->format('G') . "." . $event_date_format->format('i') ;

Any idea what I have todo to get this information out of the date?

4
  • 1
    Based on the line where you get the date data ($event_date_format = DateTime::createFromFormat('Ymd', $event_date[0]);) it seems like you are only getting the year, month, and date, and not the hours and minutes that you want. Commented Sep 26, 2016 at 12:51
  • "I can't make that work" is not a problem statement. What exactly you expect? There is no way to output "Montag" for 6th October because it is Thursday. DateTime::format('l') outputs weekday in English. You need to use IntlDateFormatter to translate it. Commented Sep 26, 2016 at 12:52
  • By the way, you can use DateTime's format() call once instead of many times with concatenations, try: $event_date_format->format('l, d. M , G.i'); which gets you much cleaner code. Commented Sep 26, 2016 at 12:54
  • To get the day in letter you can get the number of the date and with a switch case do it yourself Commented Sep 26, 2016 at 12:55

1 Answer 1

2

You'll want to set up your whole format with the formatter to get the correct output with the German language strings.

$fmt = new IntlDateFormatter('de_DE' ,IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Berlin', IntlDateFormatter::GREGORIAN);
// set the format to Samstag, 01. Oktober 2016, 14:30
$fmt->setPattern("cccc, dd. MMMM YYYY, HH:mm");

// adjusted per edit 2: you need will also need to ensure your input $event_date[0] is in a format which includes a time
$event_date_format = DateTime::createFromFormat('Ymd H:i', $event_date[0]);

// you can add the Uhr here, or add it in the format, escaping as needed
$dateOp = $fmt->format($event_date_format)." Uhr";

See this page for format options: http://userguide.icu-project.org/formatparse/datetime

Edit: sorry just noticed the requirement for the time and edited accordingly.

Edit 2: Just noticed Anton pointed out in a reply that your creation of $event_date_format doesn't include the time - absolutely correct. So the time on these events will be incorrect until you adjust your input to include the time.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.