如何使用PHP获取节假日日期

vxf3dgd4  于 2023-01-01  发布在  PHP
关注(0)|答案(7)|浏览(580)

例如:
1.* 一月的第三个星期一马丁·路德·金纪念日
1.
2月第三个星期一总统日(华盛顿生日)
1.
三月的最后一个周日复活节
1.
五月的最后一个星期一*阵亡将士纪念日
我正在努力得到这些日期,这样我就可以把它标记在我的日历上,而不需要手动把未来几年的一切。

修改答案!!

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

e3bfsja21#

您可以利用php.strtotime的这个函数

$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"));
x6yk4ghg

x6yk4ghg2#

strtetime函数在这里很有用,但是它在解释英语单词时似乎有一些特殊之处。一定要仔细检查你的用词和结果。
例如,这似乎应该工作,但它将返回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文档页面有关于其使用的附加注解。

mnowg1ta

mnowg1ta3#

这种方法对我很有效。

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

brgchamk

brgchamk4#

在德国移动假日的解决方案:返回'Y-m-d'格式的字符串数组,包括耶稣受难日、复活节星期一、ascencion和圣灵降临节星期一。为复活节每天创建一个新的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;
}
rjjhvcjd

rjjhvcjd5#

我创建了一个函数,它可以计算任何一年的所有联邦假日(美国),并将它们返回到一个数组中。我用几个不同的年份(从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;
    }
n3h0vuf2

n3h0vuf26#

如果你愿意在这里一次性手动输入日期,你可以使用我的定制函数。它简短,简单,满足每个人的需要。如果你愿意,你可以自己修改它。另外,如果你能找到一种方法,以给定的格式动态传递数组中的日期,那么你可以选择修改它。然而,我下面的功能只适用于那些有固定日期的假日。2你可能需要手动管理每年有动态或变动日期的假日,如阵亡将士纪念日、劳动节等。
我的自定义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);
e5nqia27

e5nqia277#

我综合了@Shubham Jha和@Max的解决方案,得出了以下选择:

//Function to see  if a date is a federal holiday and out put holiday name or day name
function checkHoliday($date){
    $nyDay = date('d M', strtotime("1/1")); //new years day
    $mlkDay = date('d M', strtotime("third monday of january"));  //martin luther king day
    $wbDay = date('d M', strtotime("third monday of february")); ; //washingtons birthday
    $mDay = date('d M', strtotime("last monday of May")); //memorial day  
    $iDay = date('d M', strtotime("7/4"));  // independence day
    $lDay = date('d M', strtotime("first monday of september")); //labor day
    $vDay = date('d M', strtotime("11/11")); //veterans day
    $colDay =date('d M', strtotime("second monday of october")); //columbus day   
    $tDay = date('d M', strtotime("last thursday of november")); // thanksgiving       
    $cDay = date('d M', strtotime("12/25"));  //christmas day
    $receivedDate = date('d M', strtotime($date));

    $holiday = array(
      $nyDay => 'new year\'s day',
      $mlkDay => 'martin luther king Day',
      $wbDay => 'washington\'s birthday',
      $mDay => 'memorial day',
      $mDay => 'independance day',
      $lDay => 'labor day',
      $vDay => 'veterans day',
      $colDay => 'columbus day',
      $tDay => 'thanksgiving day',
      $cDay => 'christmas day',
    );

    foreach($holiday as $key => $value){
      if($receivedDate == $key){
        return $value;
      }
    }
    if($receivedDate) {
    return strtolower(date('l', strtotime($receivedDate)));
    }
}
//end checkHoliday function

相关问题