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

How to convert date to Chinese in php

藏色散人
Release: 2023-03-14 13:46:01
Original
1607 people have browsed it

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.

How to convert date to Chinese in php

#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' ));
Copy after login

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(&#39;/(?<!^)(?!$)/u&#39;, $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 = &#39;&#39;;
        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 .= &#39;年&#39;;
            } elseif ($i == 1) {
                $str_time .= &#39;月&#39;;
            } elseif ($i == 2) {
                $str_time .= &#39;日&#39;;
            }
        }
        return $str_time;
    }
Copy after login

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!

Related labels:
php
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