matt
matt

Reputation: 44393

PHP Date formatting with name of day and time

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?

Upvotes: 1

Views: 720

Answers (1)

HeatherEMSL
HeatherEMSL

Reputation: 285

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.

Upvotes: 2

Related Questions