0

Trying to write an IF statement to show Christmas opening hours, using the formula that if todays date is between 1st December and 5th January, otherwise show the normal times. But all I'm getting is the normal times.

$xmasStart = date('m-d', strtotime('11-01'));
$xmasEnd = date('m-d', strtotime('01-05'));
if((date('m-d') > $xmasStart) && (date('m-d') < $xmasEnd)) {
    echo 'Christmas Opening Hours';
} else {
    echo '<p class="marginbottom0">Monday to Friday: 8am - 6pm<br><small>Saturday &amp; Sunday: Appointment only</small></p>';
}
Lee
  • 4,187
  • 6
  • 25
  • 71
  • In order to determine whether a date is greater than another, you need a year. January will never be greater than December if they're both in the same year, and with only a month and day they are assumed to be both in the same year. How is your code supposed to guess that you mean January of the following year? Common sense - if I hand you two pieces of paper, one saying "Jan 5" and the other "Dec 1" and ask you to order them by which one is first, what would your answer be? – Ken White Nov 24 '17 at 13:45
  • 2
    Possible duplicate of [PHP check if date between two dates](https://stackoverflow.com/questions/19070116/php-check-if-date-between-two-dates) – Tom Udding Nov 24 '17 at 13:48
  • Ken, my answer would be whichever date is given first, will be the first occurence, and the second would be in the following year - which is common sense. – Lee Nov 24 '17 at 14:01
  • @Lee Ken may no longer be present so you may need to delete your comment to him and repost it using the `@` as I did for you here. Remember to follow the `@member space message` format. – Funk Forty Niner Nov 24 '17 at 14:14
  • 1
    @Lee Have you not tried any of the answers below? Both look good to me. – Funk Forty Niner Nov 24 '17 at 14:15

2 Answers2

4

Don't use strings to do date math. Use DateTime() which is clearer and easier to understand.

DateTime() objects are comparable so you don't need to convert them to strings to do comparisons. Additionally, it is timezone and daylight savings time aware (which doesn't come into play here but may at other times you works with dates).

<?php

$xmasStart = new DateTime('11/1 00:00:00');
$xmasEnd = new DateTime('1/5 23:59:59');
$now = new DateTime();
if($now >= $xmasStart && $now < $xmasEnd) {
    echo 'Christmas Opening Hours';
} else {
    echo '<p class="marginbottom0">Monday to Friday: 8am - 6pm<br><small>Saturday &amp; Sunday: Appointment only</small></p>';
}

Additionally, I added the times to each day as DateTime, and strtottime() will use the current time and not the beginning or ending of each day so on the last day of the Xmas hours you will not show the right hours. (You can also change the last day to be 1/6 00:00:00).

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
2

strtotime doesn't understand your short time definition, try to use complete date in Y-m-d format (2017-12-01 and 2018-01-05 respectively). Please also notice that your comparison doesn't include edge dates so you may want to use <= and >= instead.

$xmasStart = date('Y-m-d', strtotime('2017-12-01'));
$xmasEnd = date('Y-m-d', strtotime('2018-01-05'));
$now = date('Y-m-d');
if(($now >= $xmasStart) && ($now <= $xmasEnd)) {
    echo 'Christmas Opening Hours';
} else {
    echo '<p class="marginbottom0">Monday to Friday: 8am - 6pm<br><small>Saturday &amp; Sunday: Appointment only</small></p>';
}
Flying
  • 4,422
  • 2
  • 17
  • 25