PHP:从easter_date()减去天以计算Ash星期三

时间:2016-06-01 05:39:18

标签: php date time

我正在尝试用PHP计算Ash Wednesday的日期,但我遇到了一个不寻常的问题。

  • 星期三在复活节前庆祝 46天
  • 一天有 86400 秒。
  • 46 * 86400 = 3974400
  • 因此,星期三在复活节前 3974400秒

twitch.isDone()是一个内置的PHP函数,它返回当年复活节午夜的Unix时间戳。

easter_date()返回date('F d, Y', easter_date())正确

现在......这就是问题的开始。

因此,可以假设March 27, 2016返回date('F d, Y', easter_date() - 3974400),对吗?

错误。

实际结果是February 10, 2016,这是在星期三之前的一天。

这可能是因为2016年是闰年吗?这是February 09, 2016的某种已知问题吗?或者我只是做错了什么?

  

编辑:我正在使用PHP版 5.5.9-1ubuntu4.16 作为记录。

1 个答案:

答案 0 :(得分:1)

您好试试这段代码,

    <?php     
    // Determine the date of Easter for a given year.
    function EasterDate($EasterYear)
    {
        $goldenNumber = fmod($EasterYear, 19) + 1;     
        if ($EasterYear <= 1752)
        {
            $dominicalNumber = fmod($EasterYear + ($EasterYear / 4) + 5, 7);
            $paschalFullMoon = fmod(3 - (11 * $goldenNumber) - 7, 30);
        }
        else
        {
            $dominicalNumber = fmod($EasterYear + ($EasterYear / 4) - ($EasterYear / 100) + ($EasterYear / 400), 7);
            $solarCorrection = ($EasterYear - 1600) / 100 - ($EasterYear - 1600) / 400;
            $lunarCorrection = ((($EasterYear - 1400) / 100) * 8) / 25;
            $paschalFullMoon = fmod(3 - 11 * $goldenNumber + $solarCorrection - $lunarCorrection, 30);
        }     
        while ($dominicalNumber < 0)
        {
            $dominicalNumber += 7;
        }     
        while ($paschalFullMoon < 0)
        {
            $paschalFullMoon += 30;
        }     
        if ($paschalFullMoon == 29 || ($paschalFullMoon == 28 && $goldenNumber > 11))
        {
            $paschalFullMoon--;
        }     
        $difference = fmod(4 - $paschalFullMoon - $dominicalNumber, 7);     
        if ($difference < 0)
        {
            $difference += 7;
        }     
        $dayEaster = $paschalFullMoon + $difference + 1;     
        if ($dayEaster < 11)
        {            
            $EasterDate = mktime(0, 0, 0, 3, $dayEaster + 21, $EasterYear);
        }
        else
        {
            $EasterDate = mktime(0, 0, 0, 4, $dayEaster - 10, $EasterYear);
        }     
        return $EasterDate;
    } 
    // Requires EasterDate()
    function dateAshWednesday($EasterYear)
    {
        $easter = getDate(EasterDate($EasterYear));
        return mktime(0, 0, 0, $easter['mon'], $easter['mday'] - 46, $easter['year']);
    }     
    // Unit tests
     $testYear = 2016;
     echo "Ash Wednesday: " . date("j F Y", dateAshWednesday($testYear)) . "<br>";
     echo "Easter: " . date("j F Y", EasterDate($testYear)) . "<br>";
?>