Reputation: 1717
The beautiful Polish language uses different grammar for the name of the months when saying things like "March 2013" (without day) vs. "March 17 2013" (with day).
Using PHP's strftime()
with %B
gives the correct name of the month for the without-day-case. How can I properly write the date in the with-day-case? Do I have to code something myself or is there already some support for such cases?
Upvotes: 4
Views: 9163
Reputation: 2907
There is IntlDateFormatter, an international version of DateFormatter since PHP5.3 if you turn on intl extension.
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
exit ('IntlDateFormatter is available on PHP 5.3.0 or later.');
}
if (!class_exists('IntlDateFormatter')) {
exit ('You need to install php_intl extension.');
}
$polishDateFormatter = new IntlDateFormatter(
'pl_PL',
IntlDateFormatter::LONG,
IntlDateFormatter::NONE
);
$now = new DateTime("2013-08-06");
echo $polishDateFormatter->format($now), "\n";
This code returns me,
6 sierpnia 2013
which I hope correct. (I know no Polish ;-)
You may also check IntlDateFormatter::SHORT, MEDIUM and FULL to get another notations at the second parameter of the constructor.
Upvotes: 8
Reputation: 2657
You need two arrays:
$m_en = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$m_pol = array("Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec");
all you have to do now is:
$workoutSlug = str_replace($m_en, $m_pol, $input);
Input is you input you wish to translate.
Upvotes: 6
Reputation: 30875
As you probably do not know all the languages in the world i would assume that such functionality is required for any language, and the English is an exception where this rule do not change anything. Then i would create on format class that, will provide valid date string depending on locale.
Your own function may implement logic as replacing the month from Marzec
into Marca
, so you store two arrays with proper index and based on month you choose what item from array one should be replaced with what item of array2 in output string before returning.
Sometimes the easiest option is the best you can do.
Upvotes: 1
Reputation: 1
If you're looking for date formating in php, here you go: http://php.net/manual/en/datetime.format.php
Upvotes: -3