search
HomeBackend DevelopmentPHP ProblemHow to convert month to Chinese in php

How to convert month to Chinese in php

Mar 06, 2023 am 09:18 AM
phpChinese

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;
    }

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);

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment