我怎么知道今天是否在 PHP 中的圣诞节和主显节之间?

Tom*_*ers 3 php date strtotime

我以为这很容易,但我发现它比我想象的要难。

每天,我都想检查今天是否在圣诞节(12 月 25 日)和主显节(1 月 6 日)之间,即圣诞节。

我遇到的问题与年份有关,因为年份在那个时期的中间发生了变化。

我的基本 if 语句是:

$this_year_today = date("d-M-Y");
$christmas = date('d-M-Y', strtotime("25 December ".$year));
$epiphany = date('d-M-Y', strtotime("6 January ".$year));

if(($this_year_today >= $christmas) && ($this_year_today < $epiphany)){
      $season= "Christmastide";}
Run Code Online (Sandbox Code Playgroud)

但是如果今天是在 2017 年圣诞节之后,那么主显节日期是 2018 年,但是如果今天是在主显节之前,那么圣诞节就是 2016 年。请稍等。最简单的方法是什么。我尝试使用 strtotime 说“去年圣诞节”,但似乎没有用...

谢谢

dec*_*eze 5

function isChristmastide() {
    $month = date('n');
    $day = date('j');
    return ($month == 12 && $day >= 25) || ($month == 1 && $day <= 6);
}

echo isChristmastide() ? 'Ho ho ho' : 'No no no';
Run Code Online (Sandbox Code Playgroud)

其他选择:

$today = date('nj');
return 1225 <= $today || $today <= 16;
Run Code Online (Sandbox Code Playgroud)

虽然我发现可读性有问题。