Hunter Turner Hunter Turner - 3 months ago 10
HTML Question

PHP Display HTML between certain dates

I have 3 different banners I'd like to display at certain dates.

<div>
<?php if(somedate1 === today) { ?>
<div class="banner-1"></div>
<?php } else if(somedate2 === today) { ?>
<div class="banner-2"></div>
<?php } else if(somedate3 === today) { ?>
<div class="banner-3"></div>
<?php } ?>
</div>


In my research, I have found many different ways to accomplish this. I am wondering what is the most efficient and modern way to select and compare dates in PHP?

Answer

To achieve this, you will have to check whether the date is before, or is after today's date, whatever you need.

To do so you can use PHP's strtotime() function

<?php
    if(strtotime('12-01-2015') > strtotime('today')) {
        //Given date is after today
    } else {
        //Given date is before today
    }
?>

In the if() the code checks if the given date, in this case 12-01-2015, is after today's date, if it's not, it will execute the code given in the else

Hope this helps!

Comments