5
<ul>
    <?php
    $event_date = get_sub_field('event_date'); // 20150203
    $event_date_format = DateTime::createFromFormat('Ymd', $event_date);
    setlocale(LC_ALL, 'de_DE');
    ?>  
    <li>
        <h6><?php echo $event_date_format->format('d');?>. <?php echo $event_date_format->format('F');?></h6>
        <p><?php echo $event_date_format->format('H');?>:<?php echo $event_date_format->format('i');?></p>
    </li>
</ul>

The output of this is

03. February 
19:25

Why does setlocale not have any impact on this. I want my Month "F" to be in german like 3. Feber 19:25

Any idea what I'm doing wrong here?


UPDATE 1:

If I try using strftime() i suddenly get a different date output. it is in german but wrong?

<ul>
        <?php
        $event_date = get_sub_field('event_date');
        $event_date_format = DateTime::createFromFormat('Ymd', $event_date);
        setlocale(LC_ALL, 'de_DE');
        ?>  
        <li>
            <h6><?php echo $event_date_format->format('d');?>. <?php echo strftime('%B', $event_date);?></h6>
            <p><?php echo $event_date_format->format('H');?>:<?php echo $event_date_format->format('i');?></p>
        </li>
    </ul>

Suddenly the output date is not 03. February but 03. August, even though the date should be February.

Any ideas?


UPDATE 2:

This is pretty weird. I just checked the variable $event_date online in a unix conversion tool and the I get this … 

$event_date: 20150203

Sat, 22 Aug 1970 05:16:43 GMT

The value is set inside a wordpress backend with a datepicker and clearly says 03/02/2015

enter image description here

matt
  • 42,713
  • 103
  • 264
  • 397

3 Answers3

7

date() and DateTime do not respect locale

use strftime()

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.date.php

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

fbas
  • 1,676
  • 3
  • 16
  • 26
  • Thank you, I updated my question with the changed ``. However I have the problem now, that my month is different for some reason? It should be "Feber" (february) and not "August" as it is output now. – matt May 18 '15 at 06:09
  • 1
    You began to use $event_date_format, but for this call you are still using $event_date, which is the string '20150302'. the strftime is treating that as an integer, in epoch time (aka Unix Timestamp, number of seconds since Jan 1 1970). You should instead use ``strftime('%B', $event_date_format->getTimestamp() )`` – fbas May 18 '15 at 14:44
  • Deprecated function in PHP 8.1. – Khom Nazid Apr 05 '22 at 01:36
2

Annoyingly, date is not locale-aware. From the manual:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date.

You probably want:

setlocale(LC_TIME, 'de_DE');
echo strftime('%B');

Alternatively, checkout Carbon. It's got a consistent API that includes locale-awareness. As an example:

setlocale(LC_TIME, 'German');
echo Carbon::now()->formatLocalized('%B'); // Sonntag
bishop
  • 37,830
  • 11
  • 104
  • 139
2
setlocale(LC_TIME,'de_DE.UTF8');
$eventDate = new DateTime(get_field('event_date'));
$eventDate_ts = $eventDate->getTimestamp();
echo gmstrftime("%B", $eventDate_ts);

Character coding is set to 'de_DE.UTF8' for German, gmstrftime is used here, also.

March to März, Mar to Mrz.

The Member function of DateTime() generates the necessary Timestamp.

LastChain
  • 21
  • 3