【问题标题】:display message a week before and a week after a set date在设定日期的前一周和后一周显示消息
【发布时间】: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 &lt; date('Y-m-d', strtotime($oneweek))) { echo 'hello world!'; }

标签: php date


【解决方案1】:
<?php

$date = date('Y').'-12-25';

$today = strtotime(date('Y-m-d'));

$dateMin = strtotime($date . " -1 week");
$dateMax = strtotime($date . " +1 week");

echo $dateMin . '<br>' . $dateMax . '<br>' . $today;

if (($today >= $dateMin) && ($today <= $dateMax)) {

    echo 'date is in range';

}

?>

【讨论】:

  • 我认为这是您的解决方案,是吗?
猜你喜欢
  • 2016-04-19
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多