PHP Date Compare

PHP Compare Dates Within a Time Period

Comparing dates with PHP can be done by taking dates based on now, or using now. For example, let's assume you want to compare all dates within the last week. The following code can be used to output all dates within 7 days. The start variable will be the date 7 days prior to the exact date right now. The $end variable is the exact date.

Getting Dates Within the Last Week

$start = date("Y-m-d", strtotime("-7 days"));
$end = date("Y-m-d");
$dates = array($start, $end, '2012-11-24', '2012-01-01');
echo $start;
echo "<br/>";
echo $end;
echo "<br/><br/><strong>Dates:</strong>";

foreach ($dates as $mydate) {
    if ($mydate >= $start && $mydate <= $end) {
        echo "<br/>" . $mydate;