如何使用PHP获取假期日期

tel*_*per 19 php calendar date strtotime

例如:

  1. 马丁·路德·金纪念日1月3日星期一
  2. 总统日2月3 日(华盛顿诞辰日)
  3. 复活节三月的最后一个星期天
  4. 阵亡将士纪念日的5月最后一个星期

我正在尝试获取这些日期,以便我可以在我的日历上标记它,而无需手动放置未来几年的所有内容.


修改后的答案!!

$curYir = date("Y");//current year

$MLK = date('Y-m-d', strtotime("january $curYir third monday")); //marthin luthor king day
$PD = date('Y-m-d', strtotime("february $curYir third monday")); //presidents day
$Est =  date('Y-m-d', easter_date($curYir))); // easter 
$MDay = date('Y-m-d', strtotime("may $curYir first monday")); // memorial day
//("may $curYir last monday") will give you the last monday of may 1967
//much better to determine it by a loop
      $eMDay = explode("-",$MDay);
      $year = $eMDay[0];
      $month = $eMDay[1];
      $day = $eMDay[2];

      while($day <= 31){
          $day = $day + 7;
      }
      if($day > 31)
      $day = $day - 7;

      $MDay = $year.'-'.$month.'-'.$day;
$LD = date('Y-m-d', strtotime("september $curYir first monday"));  //labor day
$CD = date('Y-m-d', strtotime("october $curYir third monday")); //columbus day
$TH = date('Y-m-d', strtotime("november $curYir first thursday")); // thanks giving 
//("november $curYir last thursday") will give you the last thursday of november 1967
//much better to determine it by a loop
      $eTH = explode("-",$TH);
      $year = $eTH[0];
      $month = $eTH[1];
      $day = $eTH[2];

      while($day <= 30){
          $day = $day + 7;
      }
      if($day > 30)
      //watch out for the days in the month November only have 30
      $day = $day - 7;

      $TH = $year.'-'.$month.'-'.$day;
Run Code Online (Sandbox Code Playgroud)

San*_*hit 10

你可以利用php的这个功能.的strtotime

$currentYear = date("Y");
Run Code Online (Sandbox Code Playgroud)

MLK日 -

echo date('Y-m-d', strtotime("third monday of january $currentYear"));
Run Code Online (Sandbox Code Playgroud)

总统日 -

echo date('Y-m-d', strtotime("third monday/OD February $currentYear"));
Run Code Online (Sandbox Code Playgroud)

复活节 -

echo date('Y-m-d', strtotime("last sunday of march $currentYear"));
Run Code Online (Sandbox Code Playgroud)

纪念日 -

echo date('Y-m-d', strtotime("last monday of may $currentYear"));
Run Code Online (Sandbox Code Playgroud)

  • 复活节并不总是三月的最后一个星期一。最好使用 PHP 内置的 easter_date() 函数:http://php.net/manual/en/function.easter-date.php (3认同)

Ste*_*erg 6

strtotime功能在这里很有用,但它似乎在如何解释英语词汇方面有一些特殊性.请务必仔细检查您的措辞并检查结果.

例如,这似乎应该可行,但它将返回4月的最后一个星期一($ currentYear = 2014)!

echo "Memorial Day " . date('F d, Y', strtotime("may $currentYear last monday")) . "<br />";
Run Code Online (Sandbox Code Playgroud)

但是,这个措辞将返回2014年的正确假期.

echo "Memorial Day " . date('F d, Y', strtotime("last monday of May $currentYear")) . "<br />"; 
Run Code Online (Sandbox Code Playgroud)

这将为您提供明年假期的正确日期.

echo "Memorial Day " . date('F d, Y', strtotime("last monday of May $currentYear+1")) . "<br />";
Run Code Online (Sandbox Code Playgroud)

PHP文档页面对其使用有其他评论.照顾自己


Ame*_* Ra 5

这种方式对我有用。

function observed_date($holiday){
    $day = date("w", strtotime($holiday));
    if($day == 6) {
        $observed_date = $holiday -1;
    } elseif ($day == 0) {
        $observed_date = $holiday +1;
    } else {
        $observed_date = $holiday;
    }
    return $observed_date;
}

function get_holiday($holiday_name) {

    $currentYear = date('Y');

    switch ($holiday_name) {
        // New Years Day
        case "new_year":
            $holiday = observed_date(date('Ymd', strtotime("first day of january $currentYear")));
            break;
        // Martin Luther King, Jr. Day
        case "mlk_day":
            $holiday = date('Ymd', strtotime("january $currentYear third monday"));
            break;
        // President's Day
        case "presidents_day":
            $holiday = date('Ymd', strtotime("february $currentYear third monday"));
            break;
        // Memorial Day
        case "memorial_day":
            $holiday = (new DateTime("Last monday of May"))->format("Ymd");
            break;
        // Independence Day
        case "independence_day":
            $holiday = observed_date(date('Ymd', strtotime("july 4 $currentYear")));
            break;
        // Labor Day
        case "labor_day":
            $holiday = date('Ymd', strtotime("september $currentYear first monday"));
            break;
        // Columbus Day
        case "columbus_day":
            $holiday = date('Ymd', strtotime("october $currentYear second monday"));
            break;
        // Veteran's Day
        case "veterans_day":
            $holiday = observed_date(date('Ymd', strtotime("november 11 $currentYear")));
            break;
        // Thanksgiving Day
        case "thanksgiving_day":
            $holiday = date('Ymd', strtotime("november $currentYear fourth thursday"));
            break;
        // Christmas Day
        case "christmas_day":
        $holiday = observed_date(date('Ymd', strtotime("december 25 $currentYear")));
            break;

        default:
            $holiday = "";
            break;
    }
    return $holiday;

}
Run Code Online (Sandbox Code Playgroud)

然后你可以通过调用函数打印出假期 echo get_holiday('new_year');

2015 年的结果应该是 20150101

  • 在 YYYYMMDD 格式的日期中加 1 或减 1 是一个坏主意:如果日期恰好是该月的第一天,则回滚 1 天将给出无效日期。例如,2011 年 1 月 1 日(临近元旦)是星期六,从 20110101 中减去 1 得到 20110100。如果假期恰巧是星期日的该月的最后一天,则相同:如果添加 1 天,你最终会得到一个无效的日期。 (2认同)