1

Possible Duplicate:
How to check if a date is in a given range?

I need to show some info on my site between these upcoming days:

12/28 to 1/1

Here's my current way of doing it but it looks pretty ugly:

$today = date('m/d');
$days = array('12/28', '12/29', '12/30', '12/31', '1/1');

if(in_array($today, $days)) {
    // show stuff
}

What's a cleaner way?

Community
  • 1
  • 1

2 Answers2

7

Look at the mktime() function

$dateStart = mktime(0, 0, 0, 12, 28, 2012);
$dateEnd = mktime(0, 0, 0, 1, 1, 2013);

if (time() < $dateEnd && time() > $dateStart)
    // show stuff
}
ozahorulia
  • 9,798
  • 8
  • 48
  • 72
1

I'm pretty sure you can create date objects and just compare them.

Something like this:

$today = new DateTime();
$start = new DateTime("2012-12-28");
$end = new DateTime("2012-01-01");

if($start <= $today && $today <= $end) {
    // show stuff
}

See the datetime constructor for details.

(I personally like the object oriented approach, but Hast's answer is technically faster, less memory intensive and an altogether older part of PHP code. But if you need to do anything further with the dates, like figuring out the day of the week or adding time intervals and redisplaying, you'll be glad to have the objects around.)

Patrick M
  • 10,547
  • 9
  • 68
  • 101