PHP Conditional Statement – In Between Dates

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') >= '20120120' and date('Ymd') <= '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') >= '20120120' and date('Ymd') <= '20120125'): ?>

Show the promo banner
<?php endif; ?>

Did you enjoy the post? Why not or subscribe to my RSS feed?

Leave Your Response

If you want your own icon get a gravatar.

About Schoberg

Jesse Schoberg

Jesse Schoberg is a web developer. He founded LJ Host in 2001. He has a passion CSS and other web technologies.

More About Schoberg