Page 1 of 1

Convert timestamp to show Date/Time in Simplified Chinese

Posted: Fri Mar 24, 2006 10:47 am
by Pyrite

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;	
}
Usage:

Code: Select all

echo chinesedatetime(time(), true, true);
echo chinesedatetime(time(), true, false);
echo chinesedatetime(time(), false, false);
Wish phpDN would change the default charset to UTF-8 so this would show up in Chinese in my code instead of entities.

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.

Posted: Fri Mar 24, 2006 10:57 am
by Weirdan
doesn't

Code: Select all

setlocale(LC_TIME, 'Zh_CN');
work as expected?

Posted: Fri Mar 24, 2006 11:09 am
by Pyrite
For some people, yes. For me, no. See my notes above.