note 59992 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Jan 2006 12:34:58 +0000 |
| Subject: | note 59992 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101804@lists.php.net to get a copy of this message | ||
Note Submitter: isp dot phptime at spsoft dot de
----
A Date Function Respecting Setlocale
If you are programming for a user language different from english you may end up in perfect looking
applications, spotted with some english time texts like "Tue" from the date function.
Alas date doesn't respect the setlocale, but strftime does.
For allowing the more user-friendly formatting of date() in an international environment i wrote
this date replacement which uses strftime to support the rest of the world.
<?php
function localdate($format="",$unixdate="") {#a date function respecting the
setlocale settings
if ($unixdate=="")$unixdate=time();
$hideaway=array(
'D',# #Mon through Sun
'l',# (lowercase 'L') Sunday through Saturday
'F',# January through December
'M',# Jan through Dec
);
$bringback=array(
'%a',# Mon through Sun
'%A',# Sunday through Saturday
'%B',# January through December
'%b',# Jan through Dec
);
$placeholder=array('%1','%2','%3','%4');
$format=str_replace($hideaway,$placeholder,$format); #prevent date from translating to english
$ret=date($format,$unixdate); #use date
if(strpos($ret,'%')===false) return $ret; #no placeholder,so no english text, so finished
$ret=str_replace($placeholder,$bringback,$ret); #prepare for action of strftime
return strftime($ret,$unixdate); #have the rest done by strftime
}
?>
Just set your LC_TIME to the desired language like
<?php
setlocale(LC_TIME, 'de_DE');#Zeit bitte auf deutsch
?>
in your configuration, where you preset the desired date-formatting string in the easier date
syntax.
I hope I'll never again have to code arrays with the names of the weekday-names again.
And some other PHP fellows too.
regards
Ingo