Alex
Alex

Reputation: 9265

display message a week before and a week after a set date

I am trying to echo 'Hello World' a week before and a week after in all the days leading up to/after and including December 25th every year (using this for a logo changer). Unfortunately I am stuck on the logic with the +/- 1 week part and Im not sure what to do, thus far I have it working if the date is Dec 25th.

<?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!';
}

?>

So basically the 25th is a centerpoint with a fixed one week interval both after and prior, where this should display the message.

Upvotes: 0

Views: 356

Answers (1)

Alex
Alex

Reputation: 9265

<?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';

}

?>

Upvotes: 1

Related Questions