1 回答
TA贡献2016条经验 获得超9个赞
你走在正确的轨道上。我改变了组织数据的方式,因此迭代它们更简单。
我还在DateTime()比较中使用了对象,因为它们具有可比性。无需先将它们格式化为字符串。
$jaar = date('Y');
$vakanties = [
'BeginKerst' => [
'start' => new DateTime("$jaar-01-01"),
'end' => new DateTime("$jaar-01-05"),
],
'Voorjaar' => [
'start' => new DateTime("$jaar-02-22"),
'end' => new DateTime("$jaar-03-01"),
],
'Mei' => [
'start' => new DateTime("$jaar-04-25"),
'end' => new DateTime("$jaar-05-03"),
],
'Zomer' => [
'start' => new DateTime("$jaar-07-18"),
'end' => new DateTime("$jaar-08-30"),
],
'EindKerst' => [
'start' => new DateTime("$jaar-12-21"),
'end' => new DateTime("$jaar-12-31"),
],
'Test' => [
'start' => new DateTime("$jaar-05-20"),
'end' => new DateTime("$jaar-05-25"),
],
];
$now = new DateTime();
foreach ($vakanties as $name => $vakanty) {
if ($now >= $vakanty['start'] && $now <= $vakanty['end']) {
echo "today is in the middle of a vacation: " . $name;
}
}
输出
today is in the middle of a vacation: Test
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报