php - 如何使用PHP获取假期日期

标签 php date calendar strtotime

例如:

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

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


修改后的答案!!

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

最佳答案

您可以利用 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"));

关于php - 如何使用PHP获取假期日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907561/

相关文章:

ios - CVCalendar iOS 禁用日期在更改月份时不起作用

php - 选择仅适用于第一个下拉列表的选项

php - 电子商务 |设置账单字段值

php - 如何将 MySQL foreach 循环添加到 DOMDocument XML

date - 使用 dd/mm/yyyy Access 日期查询返回零

c# - 如何创建日历事件。带有预填收件人电子邮件的 ics?

php - 将 php 头文件包含到我的索引 php 文件中 - 未生成错误

java - 日期/时间转换/算术

javascript - 为什么 Date 类返回 NaN?

android - 如何查找日历内容提供者是否存在