例如:
我正在尝试获取这些日期,以便我可以在我的日历上标记它,而无需手动将所有内容放在未来几年。
修改后的答案!!
$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;
答案 0 :(得分:8)
你可以利用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"));
答案 1 :(得分:6)
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文档页面对其使用有其他评论。 小心
答案 2 :(得分:2)
这种方式对我有用。
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
答案 3 :(得分:1)
如果您愿意在这里一劳永逸地手动输入日期,您可以在这里使用我定制的功能。它简短、简单,可满足每个人的需求。如果你愿意,你可以自己修改它。此外,如果您可以找到一种方法以给定格式动态传递数组中的日期,那么您可以选择以这种方式修改它。但是,我下面的函数仅适用于具有 STATIC 日期的假期。您可能需要手动管理每年有 DYNAMIC 或 VARYING 日期的假期,例如阵亡将士纪念日、劳动节等。
我的自定义 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);
答案 4 :(得分:0)
在德国移动瞻礼的解决方案:返回一系列的Y-m-d&#39;形成的字符串包括星期五,复活节星期一,ascencion和五旬节星期一。 为每天创建复活节ist的新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;
}
答案 5 :(得分:0)
我创建了一个函数,可以计算给定年份的所有联邦假日(美国),并将其返回到数组中。我已经对它进行了数年的测试(从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;
}