例:
私はこれらの日付を取得しようとしています。これにより、今後数年間すべてを手動で入力しなくても、カレンダーにマークを付けることができます。
修正された回答!!
$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"));
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ドキュメントページには、その使用に関する追加のコメントがあります。注意してください
この方法は私のために働いた。
_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
_になるはずです
ドイツで休日を移動するためのソリューション:聖金曜日、イースターマンデー、アセンシオン、ペンテコステ月曜日を含む「Y-m-d」形式の文字列の配列を返します。読みやすさを向上させるために、イースター用の新しい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;
}
特定の年のすべての連邦休日(US)を計算し、それらを配列内に返す関数を作成しました。私はそれをいくつかの異なる年(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;
}