PHP Conditional Statement - In Between Dates

1 minute read

Listen to article
Audio is generated by DropInBlog's AI and may have slight pronunciation nuances. Learn more

I'm always using little php snippets in tons of projects. Here's one I needed today that is very useful.  If you want a specific item to show only between a group of dates you can use this. No big function, just slap in your dates and go!

<?php if (date('Ymd') &gt;= '20120120' and date('Ymd') &lt;= '20120125' ): ?>
Show the promo banner
<?php else: ?>
Show the normal banner
<?php endif; ?>

So in English that says.. if today’s date is greater than or equal to 2012-01-20 and it’s less than or equal to 2012-01-25 show the promo banner. Otherwise show the normal banner.

Or if you only want to show the promo banner during some dates and do not have alternative content you can just end the statement like this:

<?php if (date('Ymd') &gt;= '20120120' and date('Ymd') &lt;= '20120125'): ?>
Show the promo banner
<?php endif; ?>

« Back to Blog