我如何知道 PHP 中的今天是否在圣诞节和主显节之间?
How do i know if Today is between Christmas and Epiphany in PHP?
我以为这会很容易,但我发现它比我想象的要难。
每一天,我都想检查今天是否在圣诞节(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 来表示 "last Christmas" 但似乎没有用...
感谢
未测试,但也许您可以像这样使用时间数值而不是日期来尝试?
$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;
感谢 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";}
试试这个,不要混淆年份。
$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;
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;
虽然我觉得可读性有问题。
我以为这会很容易,但我发现它比我想象的要难。
每一天,我都想检查今天是否在圣诞节(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 来表示 "last Christmas" 但似乎没有用...
感谢
未测试,但也许您可以像这样使用时间数值而不是日期来尝试?
$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;
感谢 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";}
试试这个,不要混淆年份。
$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;
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;
虽然我觉得可读性有问题。