【发布时间】:2014-12-24 05:46:29
【问题描述】:
我试图在每年 12 月 25 日之前/之后(包括 12 月 25 日)的所有日子里,在一周前和一周后回显“Hello World”(将其用于徽标更换器)。不幸的是,我被 +/- 1 周部分的逻辑困住了,我不知道该怎么做,到目前为止,如果日期是 12 月 25 日,我可以正常工作。
<?php
$xmas = date('Y').'-12-25';
$today = date('Y-m-d', strtotime(date('Y-m-d')));
if ($today == date('Y-m-d', strtotime($xmas))) {
echo 'hello world!';
}
?>
所以基本上 25 日是一个中心点,前后都有固定的一周间隔,这应该显示消息。
【问题讨论】:
-
您是否需要显示 2015 年 1 月 14-20 日和 28-3 日的消息
-
示例:
$date = date("Y-m-d");// current date然后是$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");和$date = strtotime(date("Y-m-d", strtotime($date)) . " -1 week");,这会给您 +1 周和 -1 周。 -
@Fred-ii- 是的,但在将其形成逻辑表达式时遇到问题
-
告诉我们你尝试了什么
-
试试
$xmas = date('Y').'-12-25'; $oneweek = strtotime('+1 week'); if ($xmas < date('Y-m-d', strtotime($oneweek))) { echo 'hello world!'; }