Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: date "F" with setlocale not working

<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

like image 988
matt Avatar asked Jan 09 '23 09:01

matt


1 Answers

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().

like image 169
fbas Avatar answered Jan 18 '23 15:01

fbas