How do I get locale day and month?
Solution 1
You have to use strftime()
instead of date().
Returns a string formatted according format using the given timestamp or the current local time if no timestamp is given. Month and weekday names and other language-dependent strings respect the current locale set with setlocale().
Solution 2
Use IntlDateFormatter::format
.
Example:
<?php
$df = new IntlDateFormatter('nb_NO',
IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'Europe/Oslo');
echo $df->format(time());
gives:
torsdag 16. september 2010 kl. 21.23.03 Norge
Solution 3
I know this topic is old but i had the same problem today. The dates are displayed correctly on my linux machine, but on my windows machine they are still in english.
If you want this to work on linux and windows machines you have to be careful with the setlocale() function. On linux you have to use 'nb_NO' (for example) for norwegian formats and on windows you have to use 'norwegian':
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
setlocale(LC_ALL, 'norwegian');
} else {
setlocale(LC_ALL, 'nb_NO');
}
You can find a list of language codes for Windows in the MSDN.
Steven
I'm an interaction designer with focus on usability and front-end design. 10+ years of PHP programming and 6+ years of WordPress experience. "Hardcore" programming is not my strong side as I tend to focus on usability, design and consultancy.
Updated on June 04, 2022Comments
-
Steven almost 2 years
I'm using the following function to retrieve date and time:
function formatEventTime($time, $type, $locale = 'nb_NO') { setlocale(LC_TIME, $locale); switch($type) { case 'date' : $format = '%d.%m'; break; case 'dm' : $format = '%d. %B'; break; case 'time' : $format = '%H:%M'; break; case 'dmy' : $format = '%d.%m.%Y'; break; } return strftime($format, strtotime($time)); }
wher
$time = 2010-12-03 10:00:00
. The problem is that my days and months are still in English. How do I change this to e.g. Norwegian?Update
This works on my server, but not on my locale machine. -
Matteo Riva over 13 yearsNote: requires PHP 5.3 or later
-
Steven over 13 yearsWill this affect the entire system? Or just within the function I use it?
-
zamnuts over 10 yearsHint: Use the
$pattern
argument in the constructor to use custom formatting instead of predefined ones likeIntlDateFormatter::FULL
andIntlDateFormatter::SHORT
. -- IMO, this is the best answer since (1) it does not change PHP's global locale withsetlocale
, and (2) it works cross-platform using only RFC 5646 codes - no need to translate it for Windows, and (3) it is always UTF-8 (as opposed to windows-125x when on Windows).