Joe Bobby
Joe Bobby

Reputation: 2811

Using strftime() Month Day Year or Day Month Year in Local Format

I'm using setlocale(LC_TIME, "de_DE"); to set the local time and I have <?php echo strftime("%x"); ?> in my html.

setlocale(LC_TIME, "de_DE");
<?php echo strftime("%x"); ?>

in the PHP manual for %x it says Preferred date representation based on locale, without the time Example: 02/05/09 for February 5, 2009 but using that example it will change to 05/02/09.

How do I keep the format as February 5, 2009 and if it's a country where the day comes first, display 5 February 2009 instead of switching it to 02/05/09

Upvotes: 1

Views: 128

Answers (1)

jdmcguire
jdmcguire

Reputation: 111

One way to accomplish this would be to create an array of locale values where the month comes first in the printout, then check if your locale value is in that array before printing.

Example:

$month_first_locales = array("de_DE", "fr_CA"); //add locales where date is formatted like 5 February 2009
$locale = "de_DE";
setlocale(LC_TIME, $locale);
if(in_array($locale, $month_first_locales)){
    echo strftime("%d %B %Y");
}else{
    echo strftime("%B %d, %Y");
}

Upvotes: 1

Related Questions