21

例如:

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

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


修改后的答案!!

$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;
4

6 回答 6

15

您可以利用 php.ini 的这个功能。时间

$currentYear = date("Y");

MLK 日 -

echo date('Y-m-d', strtotime("third monday of january $currentYear"));

总统日 -

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

复活节——

echo date('Y-m-d', strtotime("last sunday of march $currentYear"));

纪念日 -

echo date('Y-m-d', strtotime("last monday of may $currentYear"));
于 2013-02-16T06:28:33.473 回答
7

strtotime 函数在这里很有用,但它似乎在解释英语措辞方面有一些特殊性。请务必仔细检查您的措辞并检查您的结果。

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

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

但是,此措辞将返回 2014 年的正确假期。

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

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

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

PHP 文档页面对其使用有额外的评论。小心

于 2014-01-25T14:48:21.473 回答
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;

}

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

2015 年的结果应该是20150101

于 2015-11-05T20:45:53.340 回答
3

在德国移动圣日的解决方案:返回一组 'Ymd' 格式的字符串,包括耶稣受难日、复活节星期一、升天和五旬节星期一。每天为复活节创建一个新的 DateTime 对象,以增强可读性。

function beweglicheFeiertage( int $year ):array
{
    //Karfreitag - good friday
    $return = array();
    $easterDate = DateTime::createFromFormat('U', easter_date($year) );
    $easterDate->modify('- 1 days');
    $return[] = $easterDate->format('Y-m-d');
    //Ostermontag easter monday
    $easterDate = DateTime::createFromFormat('U', easter_date($year) );
    $easterDate->modify('+ 2 day');
    $return[] = $easterDate->format('Y-m-d');
    //Himmelfahrt ascencion
    $easterDate = DateTime::createFromFormat('U', easter_date($year) );
    $easterDate->modify('+ 40 days');//go to Ascencionday
    $return[] = $easterDate->format('Y-m-d');
    //Pfingstmontag - pentecost monday
    $easterDate = DateTime::createFromFormat('U', easter_date($year) );
    $easterDate->modify('+ 51 days');//go to Pentecost Monday
    $return[] = $easterDate->format('Y-m-d');

    return $return;
}
于 2017-11-03T16:23:45.387 回答
1

我创建了一个函数来计算任何给定年份的所有联邦假日(美国),并将它们返回到一个数组中。我已经用几个不同的年份(从 2016 年到 2024 年)对其进行了测试。以下是代码,如果您发现任何错误计算,请告诉我:

    //function that checks if a holiday lands on saturday/sunday and so we can move them to a friday/monday respectively
    private function getObservedDate($holidayDate){

        $dayofweek = date("w", strtotime($holidayDate));

        if ($dayofweek == 6) $holidayDate = date('m/d/Y', strtotime("$holidayDate - 1 days")); //saturday moves to friday
        else if ($dayofweek == 0)  $holidayDate = date('m/d/Y', strtotime("$holidayDate + 1 days"));  //sunday moves monday

        return $holidayDate;
    }


    //function that calculates the holidays for any given year
    private function getFederalHolidaysForYear($year){

        $NY = $this->getObservedDate( date('m/d/Y', strtotime("1/1/$year")) ); //new years day

        $MLK = $this->getObservedDate( date('m/d/Y', strtotime("third monday of january $year")) );  //martin luther king day

        $PD = $this->getObservedDate( date('m/d/Y', strtotime("third monday of february $year")) ); ; //presidents day

        $MDay = $this->getObservedDate( date('m/d/Y', strtotime("last monday of May $year")) ); //memorial day  

        $IDay = $this->getObservedDate( date('m/d/Y', strtotime("7/4/$year")) );  // independence day

        $LD = $this->getObservedDate( date('m/d/Y', strtotime("first monday of september $year")) ); //labor day

        $VD = $this->getObservedDate( date('m/d/Y', strtotime("11/11/$year")) ); //veterans day

        $ColD =$this->getObservedDate( date('m/d/Y', strtotime("second monday of october $year")) ); //columbus day   

        $TG = $this->getObservedDate( date('m/d/Y', strtotime("last thursday of november $year")) ); // thanksgiving       

        $CD = $this->getObservedDate( date('m/d/Y', strtotime("12/25/$year")) );  //christmas day 

        $nonWorkingDays = array();

        array_push($nonWorkingDays, $NY, $MLK, $PD, $MDay, $IDay, $LD, $ColD, $VD, $TG, $CD);

        return $nonWorkingDays;
    }
于 2019-10-09T13:58:54.720 回答
1

如果您愿意一劳永逸地在这里手动输入日期,您可以在这里使用我的定制功能。它简短,简单,可以满足每个人的需要。如果需要,您可以自己修改它。此外,如果您可以找到一种方法以给定格式动态传递数组中的日期,那么您可以选择以这种方式对其进行修改。但是,我下面的函数只适用于那些有 STATIC 日期的假期。您可能必须手动管理每年有动态或变化日的假期,例如阵亡将士纪念日、劳动节等。

我的自定义 PHP 函数

如果您的意思是检查国际假期,这里是我为它编写的一个函数。

function checkHoliday($date){
  if(date('l', strtotime($date)) == 'Saturday'){
    return "Saturday";
  }else if(date('l', strtotime($date)) == 'Sunday'){
    return "Sunday";
  }else{
    $receivedDate = date('d M', strtotime($date));

    $holiday = array(
      '01 Jan' => 'New Year Day',
      '18 Jan' => 'Martin Luther King Day',
      '22 Feb' => 'Washington\'s Birthday',
      '05 Jul' => 'Independence Day',
      '11 Nov' => 'Veterans Day',
      '24 Dec' => 'Christmas Eve',
      '25 Dec' => 'Christmas Day',
      '31 Dec' => 'New Year Eve'
    );

    foreach($holiday as $key => $value){
      if($receivedDate == $key){
        return $value;
      }
    }
  }
}

并这样称呼它

$date = '01/01/2021';

echo checkHoliday($date);
于 2021-02-05T18:47:28.587 回答