Home > Backend Development > PHP Problem > How to convert month to Chinese in php

How to convert month to Chinese in php

藏色散人
Release: 2023-03-17 22:52:01
Original
2894 people have browsed it

php method to convert month to Chinese: 1. Create a PHP sample file; 2. Convert the date and month to full Chinese through the "public function dateToChinese($date){...}" method .

How to convert month to Chinese in php

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

How to convert months to Chinese in php ?

PHP Convert date to Chinese

Some projects involve certificate generation and other functions, and the date needs to be converted to full Chinese, so this method is written

    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;
    }
Copy after login

Related introduction:

strtotime() function parses any English text date or time description into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Note: If the year representation uses a two-digit format, values ​​0-69 will be mapped to 2000-2069, and values ​​70-100 will be mapped to 1970-2000.

Note: Please note that for dates in m/d/y or d-m-y format, if the separator is a slash (/), the American m/d/y format is used. If the delimiter is a dash (-) or a dot (.), the European d-m-y format is used. To avoid potential errors, you should use YYYY-MM-DD format whenever possible or use the date_create_from_format() function.

Grammar

strtotime(time,now);
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to convert month to Chinese in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template