在PHP中添加多个日期
我目前正忙于我的头版标题。快到圣诞节或圣诞节时,添加在PHP中添加多个日期,php,Php,我目前正忙于我的头版标题。快到圣诞节或圣诞节时,添加xmas类 <?php if(date("d-m") == "25-12") { echo ' class="xmas"'; } ?> 上面的脚本工作,但是,我想添加以下日期,而不必重复脚本 <?php if(date("d-m") == "25-12" OR "26-12" OR "27-12" OR "28-12" OR "29-12" OR "30-12") { echo ' class="xmas"'; } ?&
xmas
类
<?php if(date("d-m") == "25-12") { echo ' class="xmas"'; } ?>
上面的脚本工作,但是,我想添加以下日期,而不必重复脚本
<?php if(date("d-m") == "25-12" OR "26-12" OR "27-12" OR "28-12" OR "29-12" OR "30-12") { echo ' class="xmas"'; } ?>
这不管用但这是我想要的一个例子
有人能解决这个问题吗?
<?php if(in_array(date("d-m"), ["25-12", "26-12", "27-12", "28-12", "29-12", "30-12"])) { echo ' class="xmas"'; } ?>
不过,检查日期是否在要求的范围内是最干净的选择。
检查日期是否在所需范围内是最干净的选择。首先将您感兴趣的日期放入一个数组,它们不必是连续的,甚至不必按逻辑顺序排列
$special_dates = ["25-12", "01-04", "31-12", "29-02", "21-06"];
这很容易阅读和维护
然后使用in_array()
函数
if ( in_array(date("d-m"), $special_dates) ) {
// It's a special date
} else {
// just an ordinary date
}
首先,将您感兴趣的日期放入一个数组中,它们不必是连续的,甚至不必按逻辑顺序排列
$special_dates = ["25-12", "01-04", "31-12", "29-02", "21-06"];
这很容易阅读和维护
然后使用in_array()
函数
if ( in_array(date("d-m"), $special_dates) ) {
// It's a special date
} else {
// just an ordinary date
}
我会做一点不同的事情:
<?php
if(date('m') == 12 && ($d = date('d')) >= 25 && $d <= 30)){
// do your stuff.
// Better for performance because:
// - There is no need to grab the day of the month, if the month is not december.
// - If it isn't december, the code after && is not executed.
}
function in_range($i, $start, $end){
return $i >= $start && $i <= $end;
}
if(date('m') == 12 && in_range(date('d'), 25, 30){
// do your stuff.
// Better for code reuse ability.
}
?>
我会做一些不同的事情:
<?php
if(date('m') == 12 && ($d = date('d')) >= 25 && $d <= 30)){
// do your stuff.
// Better for performance because:
// - There is no need to grab the day of the month, if the month is not december.
// - If it isn't december, the code after && is not executed.
}
function in_range($i, $start, $end){
return $i >= $start && $i <= $end;
}
if(date('m') == 12 && in_range(date('d'), 25, 30){
// do your stuff.
// Better for code reuse ability.
}
?>
总是可以查看一年中的哪一天
if(date('z') >= 358) {
...
}
或者如果你真的不想在12月31日播出:
if(date('z') >= 358 && date('z') <= 364) {
...
}
if(date('z')>=358&&date('z')始终可以检查一年中的哪一天
if(date('z') >= 358) {
...
}
或者如果你真的不想在12月31日播出:
if(date('z') >= 358 && date('z') <= 364) {
...
}
if(date('z')>=358&&date('z')“if date in range php”的点击率很多。“if date in range php”的点击率很多。谢谢。我理解范围内的日期脚本,但这是一个相当大的脚本,我正在寻找干净和简单的东西。再次感谢。谢谢。我理解范围内的日期脚本,但这是一个相当大的脚本,我正在寻找干净和简单的东西。再次感谢。我们只希望他们不是在寻找“更简单/更短”的东西这里是Smokey;-)我们只希望他们没有在那里寻找“更简单/更短”的东西(Smokey;-)