Reputation: 39638
I have a date returned from by Database (oracle) in a format like this 01-MAY-11 : and to convert the month from english to frensh (MAY to Mai) , I did this :
$date=str_replace("JAN","Janvier",$date);
$date=str_replace("FEB","Février",$date);
$date=str_replace("MAR","Mars",$date);
$date=str_replace("APR","Avril",$date);
$date=str_replace("MAY","Mai",$date);
$date=str_replace("JUN","Juin",$date);
$date=str_replace("JUL","Juillet",$date);
$date=str_replace("AUG","Août",$date);
$date=str_replace("SEPT","Septembre",$date);
$date=str_replace("OCT","Octobre",$date);
$date=str_replace("NOV","Novembre",$date);
$date=str_replace("DEC","Décembre",$date);
But i find it ugly so is there a more concise way to do this.
Thanks
Upvotes: 2
Views: 4433
Reputation: 47864
Once you set up a lookup array which relates the two languages, just use strtr()
to apply the string translation.
Code: (Demo)
$enToFr = [
"JAN" => "Janvier",
"FEB" => "Février",
"MAR" => "Mars",
"APR" => "Avril",
"MAY" => "Mai",
"JUN" => "Juin",
"JUL" => "Juillet",
"AUG" => "Août",
"SEPT" => "Septembre",
"OCT" => "Octobre",
"NOV" => "Novembre",
"DEC" => "Décembre"
];
$date = '01-MAY-11';
echo strtr($date, $enToFr);
// 01-Mai-11
If you know that you'll need to use this in multiple places in your project, you might declare it as a constant (since it won't logically change) to enjoy global scope/access.
Since it is a single native function, I don't really advise making a wrapper/helper function to perform the same process by a different function name.
Upvotes: 0
Reputation: 42458
Although the locale solutions are best, it is also possible to TO_CHAR
with the NLS_DATE_LANGUAGE
option to output this directly from Oracle:
SELECT TO_CHAR(sysdate, 'DD-MONTH-YYYY', 'NLS_DATE_LANGUAGE=French') FROM DUAL;
Upvotes: 2
Reputation: 3608
OO Style:
<?php
class FrDate extends DateTime
{
public function month($format)
{
$english = array('Jan','Febr','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');
$french = array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre');
return str_replace($english, $french, parent::format($format));
}
}
$date = new FrDate('26 Dec 2011');
echo $date->month('M'); //Output: Décembre
?>
Upvotes: 1
Reputation: 2382
You have to set your locals settings using setlocale for getting date months and days in the desired languages
here's a basic exemple:
// here's a current list of locales you can find on systems for french
$locales = array('fr_FR.utf8','fr_FR.UTF8','fr_FR.utf-8','fr_FR.UTF-8','fr_FR','fr');
setlocale(LC_TIME,$locales);
//now you can call your localized date function
If you don't want to change the locale setting for the rest of the application you can keep trace of currently setted locale and restore it after your date call like this:
// keep track of locals setting:
$loc = setlocale(LC_TIME,0);
$tmpLoc = setlocale(LC_TIME,$locales);
//your date call here and then restore locale setting
setlocale(LC_TIME,$loc);
Upvotes: 2
Reputation: 8848
use this
<?php
setlocale(LC_TIME, 'fr_FR');
echo strftime("%d %b %Y", strtotime($date)); //use your dates timestamp
?>
Upvotes: 4
Reputation: 1828
I would use array for storing translation of months:
$mc = array("jan"=>"Janvier","feb"=>"Février", ....); //(and so on)
$new_date = explode('-',$date);
$new_date[1] = $mc[strtolower($new_date[1])];
$new_date = implode('-',$new_date);
of course, you need to check if its date and make sure month is always second
Upvotes: 2