【问题标题】:How do i know if Today is between Christmas and Epiphany in PHP?我如何知道今天是否在 PHP 中的圣诞节和主显节之间?
【发布时间】:2017-05-29 08:20:51
【问题描述】:

我认为这会是一件轻而易举的事,但我发现它比我想象的要难。

每天,我都想检查今天是否在圣诞节(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";}

但是,如果今天是 2017 年圣诞节之后,那么顿悟日期是 2018 年,但如果今天是顿悟之前,那么圣诞节是 2016 年。稍微动脑筋。最简单的方法是什么。我尝试使用 strtotime 说“去年圣诞节”,但似乎没有用......

谢谢

【问题讨论】:

  • 不使用年份怎么办?如果你不使用年份,你可以比较日期并说christmasdate &lt; date &lt; epiphany。为此,我将日期格式化为M-d。这将更容易处理
  • @SacrumDeus '12-26' &lt; '12-31' &amp;&amp; '12-31' &lt; '1-6'...?这永远不会是真的……
  • 您的日期应该是1226 和1231。有了这个,你可以很容易地比较它们,因为 1231 大于 1226 而0106 小于1231。也许会有所帮助

标签: php date strtotime


【解决方案1】:
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';

其他选择:

$today = date('nj');
return 1225 <= $today || $today <= 16;

虽然我觉得可读性有问题。

【讨论】:

    【解决方案2】:

    试试这个,不用和年份混淆了。

    $current_day = date('j'); //get Day of the month without leading zeros
    $current_month = date('n'); //get Numeric representation of a month, without leading zeros
    $season = '';
    switch($current_month):
        case 1:
            $season = $current_day<=6 ? 'Christmastide' : 'Not Christmastide';            
            break;
        case 12:
            $season = $current_day>=25 ? 'Christmastide' : 'Not Christmastide';
            break;
        default: 
            $season = 'Not Christmastide';
            break;
    endswitch;
    echo $season;
    

    Demo

    【讨论】:

      【解决方案3】:

      未测试,但也许您可以尝试使用数字值而不是日期?

      $year=date('Y');
      $nextyear=$year+1;
      
      $now=time();
      $christmas = strtotime("25 December $year");
      $epiphany = strtotime("6 January $nextyear");
      
      $season=( $now >= $christmas && $now <= $epiphany ) ? 'Christmastide' : 'Not Christmastide';
      
      echo $season;
      

      【讨论】:

      • 如果你想在 1 月 3 日查看去年的圣诞节
      • 如果当前月份是 1 月,则可以考虑上一年,如果当前月份是 12 月,则可以考虑当前年份。根据该结果,您分别检查日期是大于 12 月 25 日还是小于 1 月 6 日
      【解决方案4】:

      感谢 frankiehf,这应该是现在的答案:

      $month = date('M');
      $year = date('Y');
      $last_year = $year-1;
      $next_year = $year+1;
      
      if($month == "Jan"){
        $christmas = date('d-M-Y', strtotime("25 December ".$last_year));
          $epiphany = date('d-M-Y', strtotime("6 January ".$year));}
      else{
        $christmas = date('d-M-Y', strtotime("25 December ".$year));
          $epiphany = date('d-M-Y', strtotime("6 January ".$next_year));}
      
      if(($this_year_today >= $christmas) && ($this_year_today < $epiphany)){
            $season= "Christmastide";}
      

      【讨论】:

      • 这太复杂了。
      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      相关资源
      最近更新 更多