Hunter Turner Hunter Turner - 1 year ago 70
HTML Question

PHP Display HTML between certain dates

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

<?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 } ?>

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 Source

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

    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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download