dpp89959 2013-11-13 15:51
浏览 22
已采纳

too long

I have an array structured like this:

Array ( [0] => 24-12-2013 [1] => 25-12-2013 [2] => 26-12-2014 [3] => 27-12-2013 [4])

I would like to check if any of the dates in the array are within a given date range.

The date range is structured like this:

$start = (date("d-m-Y", strtotime('25-12-2013')));
$end =   (date("d-m-Y", strtotime('26'12'2013')));

I would like to know which dates in the array are within the date range.

  • 写回答

6条回答 默认 最新

  • dongyi1777 2013-11-13 15:58
    关注

    Couple things:

    • Use timestamps or DateTime objects to compare dates, not strings
    • Use date format YYYY-MM-DD to avoid potential ambiguity about your date format (d/m/y or m/d/y)

    This code will do what you want:

    $dates = array("2013-12-24","2013-12-25","2014-12-24","2013-12-27");
    $start = strtotime('2013-12-25');
    $end =   strtotime('2013-12-26');
    
    foreach($dates AS $date) {
        $timestamp = strtotime($date);
        if($timestamp >= $start && $timestamp <= $end) {
            echo "The date $date is within our date range
    ";
        } else {
            echo "The date $date is NOT within our date range
    ";
        }
    }
    

    See it in action:

    http://3v4l.org/GWJI2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?