header('Content-Type: application/json');
$Y = 2017;
$UK_Holidays = array(
'New Year\'s Day' => array(
'start' => strtotime('30-12-'.$Y.' 00:00:00'),
'end' => strtotime('30-12-'.$Y.' 23:59:59'),
'type' => 'Bank holiday',
'Observed' => 'Default'
),
'2nd January (substitute day)' => array(
'start' => '',
'end' => '',
'type' => 'Local holiday',
'Observed' => 'Scotland'
),
);
echo json_encode($UK_Holidays, JSON_PRETTY_PRINT);
{
"New Year's Day": {
"start": 1514592000,
"end": 1514678399,
"type": "Bank holiday",
"Observed": "Default"
},
"2nd January (substitute day)": {
"start": "",
"end": "",
"type": "Local holiday",
"Observed": "Scotland"
}
}
我有更多假期要在这个PHP脚本中实现,其中一些依赖于另一个假期的日子。 1月2日(替代日),我开发了一个开关案例;
1月2日的PHP案例转换(替代日):
function calculateBankHolidays($Y) {
$bankHols = Array();
switch (date("w", strtotime("01-01-$Y 00:00:00"))) {
case 6:
$bankHols[] = "03-01-$Y";
break;
case 0:
$bankHols[] = "02-01-$Y";
break;
default:
$bankHols[] = "01-01-$Y";
}
return $bankHols;
}
输出:
[
{
"New Year's": {
"Start Date": "02-01-2017"
}
}
]
将我的案例切换到我的PHP脚本中的最佳方式是什么?
答案 0 :(得分:1)
strtotime
是您的朋友,并结合相关格式(请参阅http://php.net/manual/en/datetime.formats.relative.php)。你可以做类似的事情。
$year = date('Y');
$ts_Jan1 = strtotime("Jan 1 $year -1 day next weekday");
$ts_Jan2 = strtotime("next weekday", $ts_Jan1);
$ts2 = strtotime("first Monday Jan $year");
编辑:添加了此PHPfiddle以显示正在运行的代码。此工作代码生成的日期与OP提供的链接中表格中的日期匹配:https://www.timeanddate.com/holidays/uk/2nd-january