tel*_*per 19 php calendar date strtotime
例如:
我正在尝试获取这些日期,以便我可以在我的日历上标记它,而无需手动放置未来几年的所有内容.
修改后的答案!!
$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;
Run Code Online (Sandbox Code Playgroud)
San*_*hit 10
你可以利用php的这个功能.的strtotime
$currentYear = date("Y");
Run Code Online (Sandbox Code Playgroud)
MLK日 -
echo date('Y-m-d', strtotime("third monday of january $currentYear"));
Run Code Online (Sandbox Code Playgroud)
总统日 -
echo date('Y-m-d', strtotime("third monday/OD February $currentYear"));
Run Code Online (Sandbox Code Playgroud)
复活节 -
echo date('Y-m-d', strtotime("last sunday of march $currentYear"));
Run Code Online (Sandbox Code Playgroud)
纪念日 -
echo date('Y-m-d', strtotime("last monday of may $currentYear"));
Run Code Online (Sandbox Code Playgroud)
strtotime功能在这里很有用,但它似乎在如何解释英语词汇方面有一些特殊性.请务必仔细检查您的措辞并检查结果.
例如,这似乎应该可行,但它将返回4月的最后一个星期一($ currentYear = 2014)!
echo "Memorial Day " . date('F d, Y', strtotime("may $currentYear last monday")) . "<br />";
Run Code Online (Sandbox Code Playgroud)
但是,这个措辞将返回2014年的正确假期.
echo "Memorial Day " . date('F d, Y', strtotime("last monday of May $currentYear")) . "<br />";
Run Code Online (Sandbox Code Playgroud)
这将为您提供明年假期的正确日期.
echo "Memorial Day " . date('F d, Y', strtotime("last monday of May $currentYear+1")) . "<br />";
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以通过调用函数打印出假期 echo get_holiday('new_year');
2015 年的结果应该是 20150101
归档时间: |
|
查看次数: |
14247 次 |
最近记录: |