PHP code example of phpu / calendar

1. Go to this page and download the library: Download phpu/calendar library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpu / calendar example snippets


// 新的日历对象
$Calendar = new phpu\calendar\Calendar();

// 创建一个日历,createCalendar方法4个参数分别是:int年,int月,int日=0,int时=-1
// createCalendar方法在指定的年份不在-1000至3000之内时throw DomainException异常
$calendar = $Calendar->createCalendar(2020,5,15);

// ------------ 以下代码部分处理当天的日期转换 --------------- //

$current_gregorian_str = $calendar['w'] . ' ' . $calendar['y'] . '-' . $calendar['m'] . '-' . $calendar['d'];

$current_jq_str = !empty($calendar['solar_terms']) ? ' 节气:' . $calendar['solar_terms'][0] . ' 定:' . $calendar['solar_terms'][1] : '';

$current_gz_str = !empty($calendar['gz']) ? $calendar['gz']['y']['s'] . '年'.$calendar['gz']['m']['s'] . '月'.$calendar['gz']['d']['s'] . '日' : '';

$current_lunar_str = !empty($calendar['lunar']) ? $calendar['lunar'][0] . '年' . $calendar['lunar'][1] . $calendar['lunar'][2] : '';

print '当天:' . "\n";

print '【' . $current_gregorian_str . ' 农历:' . $current_lunar_str . ' 干支:' . $current_gz_str . $current_jq_str . '】'."\n";

print "\n";

// ------------ 以下部分是日历表数据转换 --------------- //

print '日历表:' . "\n";

foreach ($calendar['days'] as [$k,$day]){
    // 公历
    $gregorian_str = $day['gregorian']['w'] . ' ' . $day['gregorian']['y'] . '-' . $day['gregorian']['m'] . '-' . $day['gregorian']['d'];

    // 节气
    $jq_str = (isset($day['solar_terms']) && isset($day['solar_terms'][$k])) ? ' 节气:' . $day['solar_terms'][$k][0] . ' 定:' . $day['solar_terms'][$k][1] : '';

    // 干支
    $gz_str = !empty($day['gz']) ? $day['gz']['y']['s'] . '年' . $day['gz']['m']['s'] . '月' . $day['gz']['d']['s'] . '日' : '';

    // 农历
    $lunar_str = !empty($day['lunar']) ? $day['lunar'][0] . '年' . $day['lunar'][1] . $day['lunar'][2] : '';

    print $gregorian_str . ' 农历:' . $lunar_str .' 干支:'. $gz_str . $jq_str . "\n";

}



// 默认配置
    'default' => [

        // 读取日历长度
        // 0 GRID_DAY 一天
        // 1 GRID_WEEK 一周
        // 2 GRID_MONTH 一个月
        'grid' => \phpu\calendar\Calendar::GRID_MONTH,

        // 读取节气
        'solar_terms' => true,

        // 读取干支
        'heavenly_earthly' => true,

        // 读取农历
        'lunar' => true,

        // 区分早晚子时,true则 23:00-24:00 00:00-01:00为子时,否则00:00-02:00为子时
        'night_zi_hour' => false,
    ],
    // 自定义多个配置
    // 在new对象时加上参数键名作为配置名
    // 使用方法: $Calendar = new Calendar('demo');
    'demo'=>[
        'grid' => 1,
        'solar_terms' => true,
        'heavenly_earthly' => false,
        'lunar' => false,
    ],


// Julian::julianDay参数(int年,int月=1,int日=1,int时=12,int分=0,int秒=0,int毫秒=0)
$jd = phpu\calendar\Julian::julianDay(2011,8,9,5,24,35);
var_dump($jd);

/**
 * 农历月份数转中文表示
 *
 * @param int $month  农历月份数
 * @param int $isLeap 是否闰月
 *
 * @return string
 */
function lunarMonthChinese(int $month, int $isLeap = 0):string
{
    $lunar_leap = '(闰)';
    $lunar_months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
    
    if($month < 1 || $month > 12){
        return '';
    }

    $leapstr = $isLeap ? $lunar_leap : '';

    return $leapstr . $lunar_months[$month - 1];
}

/**
 * 农历日数字转中文表示
 *
 * @param int $day 农历的日数
 *
 * @return string 中文表示法 如:初五,初十,二十,廿五
 */
function lunarDayChinese(int $day):string
{
    $lunar_number = ['日', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
    $lunar_whole_tens = ['初', '十', '廿'];
    
    
    // 农历每月的天数不能超过30
    if ($day < 1 || $day > 30){
        return '';
    }

    $daystr = '';

    switch ($day){
        case 10 : $daystr = $lunar_whole_tens[0] . $lunar_number[10]; // 初十
            break;
        case 20 : $daystr = $lunar_number[2] . $lunar_number[10];     // 二十
            break;
        case 30 : $daystr = $lunar_number[3] . $lunar_number[10];     // 三十
            break;
        default:
            $k = $day / 10;
            $m = $day % 10;
            $daystr = $lunar_whole_tens[$k] . $lunar_number[$m];
    }

    return $daystr;
}