Reputation: 837
I'm trying to get the output on my second piece of code to be Dutch. The first code gives the right output, but in English. I tried to get it in Dutch but usually i ended up with 01-01-1970, 12-12-2015 or nothing. The code should give next friday this week, unless its past monday, then it's fridat next week.
My working code but in English.
<?php
$d = strtotime('today');
switch ($d) {
case strtotime('monday'):
case strtotime('friday'):
case strtotime('saturday'):
case strtotime('sunday'):
$ff = strtotime('next friday');
$ffn = date('l d M', $ff);
echo $ffn;
break;
case strtotime('tuesday'):
case strtotime('wednesday'):
case strtotime('thursday'):
$ff = strtotime('next friday' . '+ 7days');
$fft = date('l d M', $ff);
echo $fft;
break;
default:
echo "Something went wrong";
}
?>
My code which gives a Dutch output, but the wrong date (it gives today's date, not next friday/friday next week.)
By the way, this is my first question ive asked here, so I might be a bit vague or something ;)
Upvotes: 4
Views: 6724
Reputation: 837
I used str_replace instead now. It was (for me) an easy and fast way to get the output i needed. But I've only been programming for nearly a year now, so probably my way of fixing it was not the best one. But it works for now (: Thanks to eveyone who Replied to the thread!
This is what i got now using str_replace.
<?php
$d = strtotime('today');
setlocale (LC_TIME,'NL_nl');
ini_set('intl.default_locale', 'nl_NL');
switch ($d) {
case strtotime('monday'):
case strtotime('friday'):
case strtotime('saturday'):
case strtotime('sunday'):
$ff = strtotime('next friday');
$ffn = date('l d F ', $ff);
$dutch = str_replace (
array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'),
array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'),
$ffn);
echo $dutch;
break;
case strtotime('tuesday'):
case strtotime('wednesday'):
case strtotime('thursday'):
$ff = strtotime('next friday' . '+ 7days');
$fft = date('l d F', $ff);
$dutch = str_replace (
array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'),
array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'),
$fft);
echo $dutch;
break;
default:
echo "Something went wrong";
}
?>
Upvotes: 3
Reputation: 72177
strftime()
is the only PHP date & time function that supports localization.
Both date()
and strtotime()
use only English.
In order to have help strftime()
helps you, you need to use setlocale()
first.
Unfortunately, the second argument of setlocale()
is not standardized, its value depends up to some extent on the operation system where the PHP code runs.
On Unix-like systems (Linux, OSX), use:
/* Set locale to Dutch */
setlocale(LC_TIME, 'nl_NL');
/* Output: woensdag 3 juni 2015 */
echo strftime("%A %e %B %Y", strtotime('2015-06-03'));
On Windows, use:
/* Set locale to Dutch */
setlocale(LC_TIME, 'nld_nld');
The above code snippets are copied from the documentation of setlocale()
. I changed the date and tested the first example (on Mac OSX) but I don't have a Windows system at hand to check if the second one works as advertised. Anyway, I can tell from past experience that the string you need to use on Windows is different than you use on Unix-like systems. If you need to run the code on Windows you can find useful links at the end of the documentation page.
Upvotes: 2
Reputation: 4045
Just output the date using strftime() and set_locale() functions. No need to change your code logic if it is correct.
<?php
$d = strtotime('today');
$format = '%A %d %b';
setlocale(LC_TIME, 'NL_nl');
setlocale(LC_ALL, 'nl_NL');
switch ($d) {
case strtotime('monday'):
case strtotime('friday'):
case strtotime('saturday'):
case strtotime('sunday'):
$ff = strtotime('next friday');
$ffn = strftime($format, $ff);
echo $ffn;
break;
case strtotime('tuesday'):
case strtotime('wednesday'):
case strtotime('thursday'):
$ff = strtotime('next friday' . '+ 7days');
$fft = strftime($format, $ff);
echo $fft;
break;
default:
echo "Something went wrong";
}
?>
Upvotes: 6