[英]Add Case Switch To PHP Array()
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"
}
}
I have a lot more holidays to implement into this PHP script, some of which are dependant on the day another holiday lands on. 我有更多的假期可以实现到此PHP脚本中,其中一些假期取决于另一个假期的到来日期。 For 2nd January (substitute day), I've developed a switch case;
在1月2日(替代日),我开发了一个开关盒;
PHP Case Switch For 2nd January (substitute day): 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;
}
Outputs: 输出:
[
{
"New Year's": {
"Start Date": "02-01-2017"
}
}
]
What is the best way to implement my case switch into my PHP Script? 将案例切换实现到PHP脚本中的最佳方法是什么?
strtotime
is your friend here, combined with the relative formats (see http://php.net/manual/en/datetime.formats.relative.php ). strtotime
是您的朋友,结合了相关格式(请参阅http://php.net/manual/zh/datetime.formats.relative.php )。 You can do things like. 您可以做类似的事情。
$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");
EDIT: Added this PHPfiddle to show working code. 编辑:添加此PHPfiddle以显示工作代码。 The dates generated by this working code match the dates in the table at the link provided by OP: https://www.timeanddate.com/holidays/uk/2nd-january
此工作代码生成的日期与OP提供的链接中表中的日期匹配: https : //www.timeanddate.com/holidays/uk/2nd-january
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.