PHP method to convert date to JDE's Confucian calendar format

不言
Release: 2023-04-02 12:54:01
Original
2037 people have browsed it

This article mainly introduces the method of converting dates into JDE's Confucian calendar format in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it

JDE's Confucian calendar format The calendar format rules are as follows:

The Confucian calendar is a 6-digit number; (Example: January 1, 2018 => 118001)

The first digit indicates the century (Example: 1 indicates the 21st century; 0 means the 20th century);

The second and third digits represent the year (for example: 2018 is 18);

The last three digits represent the day of the year;

The method is as follows:

/**
 * 日期转换为 Jde 儒日历 【支持时间转换范围:1970 - 2999年】
 * 【六位: 第一位标识 世纪 例: 0表示20世纪,1表示21世纪,第二、三位表示年,最后三位表示本年的第几天】
 * 例:2018-01-01  =》 118001
 * @param $date
 * @return string */function getJdeDate($date){    #转换为时间戳
    $unix_time = strtotime($date);    #获取时间信息
    $ary_date = getdate($unix_time);    #获取年
    $str_year = $ary_date['year'];    #获取世纪标识 【20世纪 => 0; 21世纪 => 1; 22世纪 => 2】
    #如果年/100有余数
    if($str_year%100) {        
    $century = ceil($str_year/100)%10; #向上取整
    } else {        
    $century = floor($str_year/100)%10; #向下取整    
    }    #获取年后两位
    $year = substr($str_year,2);    #获取一年中的第几天
    $year_day = $ary_date['yday'] + 1;    #如果不足三位数补足三位数
    $year_day = str_pad($year_day,3,0,STR_PAD_LEFT);    #儒日历
    return $century.$year.$year_day;
}
Copy after login

Test case:

<?php
/**
 * 日期转换为 Jde 儒日历 【支持时间转换范围:1970 - 2999年】
 * 【六位: 第一位标识 世纪 例: 0表示20世纪,1表示21世纪,第二、三位表示年,最后三位表示本年的第几天】
 * 例:2018-01-01  =》 118001
 * @param $date
 * @return string
 */
function getJdeDate($date){
    #转换为时间戳
    $unix_time = strtotime($date);
    #获取时间信息
    $ary_date = getdate($unix_time);
    #获取年
    $str_year = $ary_date[&#39;year&#39;];
    #获取世纪标识 【20世纪 => 0; 21世纪 => 1; 22世纪 => 2】
    #如果年/100有余数
    if($str_year%100) {
        $century = ceil($str_year/100)%10; #向上取整
    } else {
        $century = floor($str_year/100)%10; #向下取整
    }
    #获取年后两位
    $year = substr($str_year,2);
    #获取一年中的第几天
    $year_day = $ary_date[&#39;yday&#39;] + 1;
    #如果不足三位数补足三位数
    $year_day = str_pad($year_day,3,0,STR_PAD_LEFT);
    #儒日历
    return $century.$year.$year_day;
}
echo getJdeDate(&#39;2018-01-01 00:00:00&#39;);
Copy after login

Output:

118001
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's learning. More related content Please pay attention to PHP Chinese website!

Related recommendations:

php How to add the instant push function

php’s two-way queue code

The above is the detailed content of PHP method to convert date to JDE's Confucian calendar format. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!