note 59992 added to function.date
| From: | ispdotphptimeatspsoftdotde at osu1 dot php dot net | Date: | Thu, 22 Dec 2005 10:02:51 +0000 |
| Subject: | note 59992 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100562@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59992
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59992&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59992&report=yes
Search -- http://master.php.net/manage/user-notes.php