'Translating PHP date() for Multilingual Site
I'm trying to create a conditional translation of the PHP internal date() function. Is it possible to somehow redefine the internal variables - e.g. - date('M'), date('y') etc so that different strings are fed into the remainder of the PHP function on the basis of this test:
if (ICL_LANGUAGE_CODE == 'fr') { }
The following is a working example of the code I'm using for a dates module. Since $date is defined with many variables contained in this definition it's important to conditionally re-define the variables within PHP's date() first in order to avoid having to redefine the variable 100 times or more within each key.
if($start <= $end):
if($start == $end):
//Month Day, Year
$date = date('F', $start).' '.date('j',$start).', '.date('Y', $start);
else:
if($start_year == $end_year):
if($start_month == $end_month):
//Month Day - Day, Year
$date = date('F', $start).' '.date('j',$start).' - '.date('j', $end).', '.date('Y', $start);
else:
//Month Day - Month Day, Year
$date = date('F', $start).' '.date('j',$start).' - '.date('F', $end).' '.date('j', $end).', '.date('Y', $start);
endif;
else:
//Month Day, Year - Month Day, Year
$date = date('F', $start).' '.date('j',$start).', '.date('Y', $start).' - '.date('F', $end).' '.date('j', $end).', '.date('Y', $end);
endif;
endif;
endif;
Solution 1:[1]
$date = date('F', $start).' '.date('j',$start).', '.date('Y', $start);
That's a rather painful way to go about. The format string in date() doesn't have to be a single character. This line could be reduced to
$date = date('F j Y');
And given that, you could have a simple
switch($whats_my_locale) {
case 'FR':
$format = 'date format characters for a french date';
break
case 'EN' :
$format = 'format chars for english date'
break
case etc....
default:
$format = 'default date format string here';
}
$local_date_string = date($format, $start);
and off you go.
Solution 2:[2]
I'm sure you have, but have you considered just using the numeric values?
Also, if you do use them, remember the US has months / day, opposite to the UK and others.
Solution 3:[3]
Can be done rather easily with either define or a switch.
DEFINE (PHP 7)
<?php
// With Define
$untranslated_date = $date('D');
translate_date($untranslated_date);
function translate_date($untranslated_date)
{
define('translated_days', array(
'Mon' => 'Ma',
'Tue' => 'Di',
'Wed' => 'Wo',
'Thu' => 'Do',
'Fri' => 'Vr',
'Sat' => 'Za',
'Sun' => 'Zo')
);
echo translated_days[$untranslated_date];
}
SWITCH (or if statement)
// With a switch (or if)
$untranslated_date = $date('D');
translate_date($untranslated_date);
function translate_date($untranslated_date)
{
switch ($untranslated_date)
{
case "Mon":
$translated_date = "Ma";
break;
case "Tue":
$translated_date = "Di";
break;
case "Wed":
$translated_date = "Wo";
break;
case "Thu":
$translated_date = "Do";
break;
case "Fri":
$translated_date = "Vr";
break;
case "Sat":
$translated_date = "Za";
break;
case "Sun":
$translated_date = "Zo";
break;
}
echo $translated_date;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marc B |
| Solution 2 | Jake Lee |
| Solution 3 | Chris Baaijens |
