项目中,有些涉及到证书生成等功能,需要将日期转换为全中文,故写此方法
public function dateToChinese($date)
{
$chineseDate = '';
//$date = '2018-10-29'
if (false == empty($date)) {
$chineseArr = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');//把数字化为中文
$chineseTenArr = array('', '十', '二十', '三十');//十位数对应中文
$year = date('Y', strtotime($date));
$month = date('m', strtotime($date));
$day = date('d', strtotime($date));
//转换为数组
$yearArr = str_split($year);
foreach ($yearArr as $value) {
$chineseDate .= $chineseArr[$value];
}
$chineseDate .= '年';
$monthArr = str_split($month);
//月,日去除零
if ($monthArr[1] != 0) {
$chineseDate .= $chineseTenArr[$monthArr[0]] . $chineseArr[$monthArr[1]] . '月';
} else {
$chineseDate .= $chineseTenArr[$monthArr[0]] . '月';
}
$dayArr = str_split($day);
if ($dayArr[1] != 0) {
$chineseDate .= $chineseTenArr[$dayArr[0]] . $chineseArr[$dayArr[1]] . '日';
} else {
$chineseDate .= $chineseTenArr[$dayArr[0]] . '日';
}
}
return $chineseDate;
}