PHP is a very popular server-side scripting language that can be used to build web applications, manage databases, generate dynamic web pages, process form data, etc. For many Chinese programmers, they especially need to convert time into Chinese. How to convert date into Chinese in PHP is a very practical skill.
1. Date formatting function
PHP provides a function for date formatting: date()
. This function can very conveniently format the date into a specified string, in which you can use PHP to convert the date into Chinese.
The formatting syntax is as follows:
date(format, timestamp)
Among them, the format
parameter is the date format string, which can specify the year, month, day, hour, minute, second, etc. A date format, the timestamp
parameter is an optional timestamp, and the default is the current time.
2. How to convert dates to Chinese in PHP
In order to convert dates to Chinese, you need to use some Chinese numbers and Chinese characters. Constants in PHP can be used to store these values.
define('CN_NUM', array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九')); define('CN_YEAR', '年'); define('CN_MONTH', '月'); define('CN_DAY', '日');
Next, we can use these constants and combine it with the date()
function to convert the date to Chinese:
function cnDate($date) { $cn_num = CN_NUM; $cn_year = CN_YEAR; $cn_month = CN_MONTH; $cn_day = CN_DAY; $year = date('Y', strtotime($date)); $month = date('m', strtotime($date)); $day = date('d', strtotime($date)); $result = ''; // 转换年份 for ($i=0; $i<strlen($year); $i++) { $result .= $cn_num[$year[$i]]; } $result .= $cn_year; // 转换月份 if ($month[0] == '0') { // 去掉月份前的0 $result .= $cn_num[$month[1]] . $cn_month; } else if ($month == '10') { // 十月 $result .= '十' . $cn_month; } else if ($month > '10') { // 十一月、十二月 $result .= $cn_num[1] . $cn_num[$month[1]] . $cn_month; } else { // 一月、二月、...、九月 $result .= $cn_num[$month[0]] . $cn_month; } // 转换日期 if ($day[0] == '0') { // 去掉日期前的0 $result .= $cn_num[$day[1]] . $cn_day; } else if ($day == '10') { // 十日 $result .= '十' . $cn_day; } else if ($day > '10' && $day < '20') { // 十一日到十九日 $result .= $cn_num[1] . $cn_num[$day[1]] . $cn_day; } else if (substr($day, 1) == '0') { // 二十、三十日 $result .= $cn_num[$day[0]] . $cn_num[10] . $cn_day; } else if ($day >= '20') { // 二十一日到二十九日 $result .= $cn_num[$day[0]] . $cn_num[10] . $cn_num[$day[1]] . $cn_day; } else { // 一日、二日、...、九日 $result .= $cn_num[$day[0]] . $cn_day; } return $result; }
3. Code explanation
First, Chinese numbers and Chinese character constants are defined, where CN_NUM
is an array containing Chinese numbers from 0
to 9
.
Next, the cnDate()
function is defined. This is a function that converts dates into Chinese format. It receives a parameter $date
, which represents the date to be converted. . Within the function, use the strtotime()
function to convert the date string into a timestamp, and then use the date()
function to extract the year, month, and day.
Then, use different rules to convert to Chinese according to the values of year, month, and day: the year directly uses Chinese numbers; the month and date are slightly more complicated, and you need to consider whether there is a prefix 0
, whether it is 10
, etc.
Finally, combine the converted year, month, and day to return a date string in Chinese format.
4. Examples
The following are some examples of using the cnDate()
function to convert dates to Chinese format:
echo cnDate('2022-08-08'); // 二零二二年八月八日 echo cnDate('2023-01-01'); // 二零二三年一月一日 echo cnDate('2024-06-10'); // 二零二四年六月十日 echo cnDate('2025-12-25'); // 二零二五年十二月二十五日
5. Summary
It is not difficult to convert dates to Chinese format in PHP. You only need to define Chinese numbers and Chinese character constants, and then combine them according to different situations to get a date string in Chinese format. This is very applicable to some Chinese websites and applications and is worth learning and mastering.
The above is the detailed content of How to convert Chinese date in php. For more information, please follow other related articles on the PHP Chinese website!