Today, PHP is used to calculate the addition and subtraction of the Lunar calendar (lunar calendar/old calendar), because this is not a calendar is not universal, so it is not like strtotime to directly add and subtract the time stamp.

But it works in a similar way, with an extra step.

Lunar calendar (Lunar calendar)

Lunar is a free, open source, typescript, javascript, Java, c#, PHP, python, go, and other calendar tools that support both solar and lunar calendars.

Currently support the Gregorian calendar and the lunar calendar, constellation, dry branch, zodiac, solar terms, festivals, Pengzu Baiji, daily should avoid, Auspicious god should be avoided, evil evil should avoid, auspicious God location, chong evil spirit, na Yin, stars, eight characters, five elements, ten gods, build in addition to twelve value stars, Qinglong Hall and other twelve gods, zodiacal and black days and auspicious and ominous.

Old version supports the span of time: 1901-01-01 (boxer during 11 years) and 2099-12-31 (not during 20), v1.2.0 and above version support from 0001 to 9999.

Github address: github.com/6tail/lunar…

You can add or subtract x days from the solar or lunar calendar, which is the next (x) function, but it doesn’t work very well. Based on this class, I wrote a simple logic for adding or subtracting dates from Lunar (Lunar/old) +n year m month R day:

include ("Lunar.php");
use com\nlf\calendar\util\HolidayUtil;
use com\nlf\calendar\Lunar;
use com\nlf\calendar\Solar;

$solar = Solar::fromYmd(2021.04.28);
// Get the lunar object based on the solar calendar
$lunar = $solar->getLunar();
$ly = $lunar->getYear();
$lm = $lunar->getMonth();
$ld = $lunar->getDay();
// Add 1 year to the corresponding lunar calendar
$nextLunar = Lunar::fromYmd($ly + 1.$lm.$ld);
$nextLunarStr = $nextLunar->toString();
$nextSolar = $nextLunar->getSolar()->toString();

echo $nextLunarStr.'<br>';// March 17, 2002
echo $nextSolar.'<br>';/ / 2022-04-17
Copy the code

The principle is simple: the corresponding year, month and day can be added or subtracted, which makes up for the deficiency that next can only add or subtract days.

Strtotime (“+x year”,time()); // This kind of one-step, later think of the above way of separating and then processing can also be, the above encapsulation can also be in one step.

However, this has limitations, for example, the number of days can not be added or subtracted beyond the range (beyond this month), it is recommended to use the next function, the number of months can not exceed the number of years (this year), need to manually judge.

Add/subtract functions calculated according to certain rules, and strtotime function types, except that the time/minute/second is not written to add/subtract:

Function encapsulation:/** * Solar calendar number of days, months, weeks, years *@param$express string expression *@param$lunarYmd String Lunar date *@return array
 */
function cal_lunar($express.$lunarYmd)
{
    // Split year, month, day, minute, second
    list($ly.$lm.$ld) = explode(The '-', date('Y-m-d', strtotime($lunarYmd)));
    // Remove two blank strings
    $express = trim($express);   
    if (stripos($express.'d')! = =false) {
        // Retrieve the symbol,"+" or "-"
        $symbol = substr($express.0.1);
        $num = intval(trim(str_replace(['day'.'days'.'d'.'+'.The '-'].' '.$express)));
        $nextLunar = Lunar::fromYmd($ly.$lm.$ld);
        $nextLunar = $symbol= ='+' ? $nextLunar->next($num) : $nextLunar->next(-$num);
        $lunar = $nextLunar->getYear() . The '-' . $nextLunar->getMonth() . The '-' . $nextLunar->getDay();
        $nextLunarStr = $nextLunar->toString();
        $nextSolar = $nextLunar->getSolar()->toString();
    } elseif (stripos($express.'w')! = =false) {
        // Retrieve the symbol,"+" or "-"
        $symbol = substr($express.0.1);
        $num = intval(trim(str_replace(['week'.'w'.'weeks'.'+'.The '-'].' '.$express)));
        // Convert to days of the week
        $num = 7 * $num;
        $nextLunar = Lunar::fromYmd($ly.$lm.$ld);
        $nextLunar = $symbol= ='+' ? $nextLunar->next($num) : $nextLunar->next(-$num);
        $lunar = $nextLunar->getYear() . The '-' . $nextLunar->getMonth() . The '-' . $nextLunar->getDay();
        $nextLunarStr = $nextLunar->toString();
        $nextSolar = $nextLunar->getSolar()->toString();
    } elseif (stripos($express.'m')! = =false) {
        // Retrieve the symbol,"+" or "-"
        $symbol = substr($express.0.1);
        $num = intval(trim(str_replace(['month'.'months'.'m'.'+'.The '-'].' '.$express)));

        if ($symbol= ='+') {
            $addNum = $lm + $num;
            if ($addNum< =12) {
                $lm = $addNum;
            } else {
                $ly = $ly + intval($addNum / 12);
                $lm = $addNum % 12; }}else {
            if ($num < $lm) {
                $lm = $lm - $num;
            } else {
                $lm = 12 - ($num - $lm) % 12;
                $ly = $ly - intvaL(($num + $lm) / 12); }}$nextLunar = Lunar::fromYmd($ly.$lm.$ld);
        $nextLunarStr = $nextLunar->toString();
        $lunar = $nextLunar->getYear() . The '-' . $nextLunar->getMonth() . The '-' . $nextLunar->getDay();
        $nextSolar = $nextLunar->getSolar()->toString();

    } elseif (stripos($express.'y')! = =false) {
        // Retrieve the symbol,"+" or "-"
        $symbol = substr($express.0.1);
        $num = intval(trim(str_replace(['year'.'years'.'y'.'+'.The '-'].' '.$express)));
        $nextLunar = Lunar::fromYmd($symbol= ='+' ? $ly + $num : $ly - $num.$lm.$ld);
        $nextLunarStr = $nextLunar->toString();
        $lunar = $nextLunar->getYear() . The '-' . $nextLunar->getMonth() . The '-' . $nextLunar->getDay();
        $nextSolar = $nextLunar->getSolar()->toString();
    } else {
        // Convert directly to return
        $nextLunar = Lunar::fromYmd($ly.$lm.$ld);
        $nextLunarStr = $nextLunar->toString();
        $lunar = $ly . The '-' . $lm . The '-' . $ld;
        $nextSolar = $nextLunar->getSolar()->toString();
    }

    return [
        'lunar'= >$lunar./ / the lunar calendar
        'lunarStr'= >$nextLunarStr.// The lunar calendar in Chinese
        'solar'= >$nextSolar./ / the Gregorian calendar
    ];
}

print_r(cal_lunar('-2 month'.20210317));


Array    
(    
[lunar] => 2021-1-17[lunarStr] => January 17, 2001 [solar] =>2021-02-28    
)
Copy the code

It is easy to add and subtract the year, month and day by the lunar function encapsulated above, for example

Cal_lunar (‘ – 2 month, 20210317); // The current lunar calendar subtracts 2 months

Cal_lunar (‘ – 99 month, 20210317); // Subtract 99 months from the current lunar calendar

Cal_lunar (‘ + 5 month, 20210317); // The current lunar calendar adds 5 months

Cal_lunar (‘ + 99 day, 20210317); // The current lunar calendar increases by 99 days

Cal_lunar (‘ – 6 week, 20210317); // The current lunar calendar subtracts 6 weeks

This is a function written by lunar.php. If you have any problems, please leave a message.