Convert timestamp to show Date/Time in Simplified Chinese

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Convert timestamp to show Date/Time in Simplified Chinese

Post 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.
Last edited by Pyrite on Fri Mar 24, 2006 11:06 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

doesn't

Code: Select all

setlocale(LC_TIME, 'Zh_CN');
work as expected?
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

For some people, yes. For me, no. See my notes above.
Post Reply