Convert timestamp to show Date/Time in Simplified Chinese
Posted: Fri Mar 24, 2006 10:47 am
Code: Select all
function chinesedatetime($timestamp, $showtime, $showtz) {
$year = "年";
$month = "月";
$day = "日";
$pm = "下午";
$am = "上午";
$mst = "美国山区时区"; // Mountain Standard Time
$chinesedatetime = date("Y",$timestamp)." ".$year." ".date("n",$timestamp)." ".$month." ".date("j",$timestamp)." ".$day;
if ($showtime) {
$chinesedatetime .= " ".date("H:i",$timestamp)." ";
$chinesedatetime .= (date("A",$timestamp) == "PM") ? $pm : $am;
}
if ($showtz) {
$chinesedatetime .= " (".$mst.")";
}
return $chinesedatetime;
}Code: Select all
echo chinesedatetime(time(), true, true);
echo chinesedatetime(time(), true, false);
echo chinesedatetime(time(), false, false);For my source file, you can look on my site at:
http://www.pysquared.com/files/chinesedatetime.phps
Now I know what you are thinking, won't setlocale() do the same kind of thing. Yes and no. setlocale differs from OS to OS, and requires a restart (on windows at least) of Apache if you change locales again. This isn't useful if you have a site where you change languages often, calling setlocale based on the language your end-user browses your site with won't work that way. So, if you have a website, which will and only ever will run on the same server with the same OS and your website is only in one language, using this function probably is of no use to you. But for me, it IS required. Just sharing the knowledge.