Home > Article > Backend Development > How to convert date to Chinese in php
php method to convert date to Chinese: 1. Create a PHP sample file; 2. Convert date to Chinese characters through the "private function toDateChinese($date){...}" method.
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
php How to convert date to Chinese?
PHP Date to Chinese Characters
$date_str = $this -> toDateChinese ( date ( 'Ym-d' ));
Final effect: October 17, 2018
private function toDateChinese($date) { $date_arr = explode('-', $date); $arr = []; foreach ($date_arr as $index => &$val) { if (mb_strlen($val) == 4) { $arr[] = preg_split('/(?<!^)(?!$)/u', $val); } else { if ($val > 10) { $v[] = 10; $v[] = $val % 10; $arr[] = $v; unset($v); } else { $arr[][] = $val; } } } $cn = array("一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "零"); $num = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "0"); $str_time = ''; for ($i = 0; $i < count($arr); $i++) { foreach ($arr[$i] as $index => $item) { $str_time .= $cn[array_search($item, $num)]; } if ($i == 0) { $str_time .= '年'; } elseif ($i == 1) { $str_time .= '月'; } elseif ($i == 2) { $str_time .= '日'; } } return $str_time; }
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to convert date to Chinese in php. For more information, please follow other related articles on the PHP Chinese website!