如何使用php获取假期日期

2022-08-30 19:57:39

例如:

  1. 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;

答案 1

你可以利用这个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"));

答案 2

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

例如,这似乎应该有效,但它将在四月的最后一个星期一返回($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文档页面有关于其使用的其他评论。当心


推荐