-1

Possible Duplicate:
How to check if a date is in a given range?
How to check if date(entered by user) is in given range (Date format :-day month ie.:-1 june )

I am trying to find whether a date is in defined range. I'm using the following code:

    $apple='25 March';
    $udate= date('d F',strtotime($apple));

    echo $udate;
    $startDate='21 March';
    $realStartDate= date('d F',strtotime($startDate)) ;
    echo $realStartDate;
    $endDate='19 April';
    $realEndDate= date('d F',strtotime($endDate)) ;
    if ($udate >= $realStartDate && $udate <= $realEndDate ) {
        echo 'within tange';
    }
    else{
        echo 'Not in range';
    }
    ?>

Where am I going wrong?

4

5 回答 5

2

试试这个它的工作......

<?php
        $udate          = '25 March';
        $udateTimestamp = strtotime($udate);


        $startDate          = '21 March';
        $startDateTimestamp = strtotime($startDate);

        $endDate           = '19 April';
        $eEndDateTimestamp = strtotime($endDate);

        if ($udateTimestamp >= $startDateTimestamp && $udateTimestamp <= $eEndDateTimestamp)
        {
                echo 'within tange';
        }
        else
        {
                echo 'Not in range';
        }
?>
于 2012-04-26T10:03:52.617 回答
1

Compare timestamps not the string representations!

if(strtotime($apple) < strtotime($endDate) && strtotime($apple) > strtotime($startDate)){
 // All ok!
}
于 2012-04-26T09:44:55.127 回答
0

您可以使用DateTime

$userDate = new DateTime("2012-03-01");

if ( $userDate > new DateTime("2012-03-21 00:00:00") &&  $userDate < new DateTime("2012-04-19 23:59:59"))
{
   // In Range 
}

如果格式为(7 月 1 日),则将其放入函数中

if (inRange ( "1 June", "3 March", "7 December" )) {
    echo "In Range";
} else {
    echo "Out Of Range";
}

function inRange($dateCheck, $dateFrom, $dateTo) {

    $date = DateTime::createFromFormat ( "d F", $dateCheck );
    $date1 = DateTime::createFromFormat ( "d F", $dateFrom );
    $date2 = DateTime::createFromFormat ( "d F", $dateTo );

    if ($date > $date1 && $date < $date2) {
        return true;
    }

    return false;

}
于 2012-04-26T07:46:28.633 回答
0

像这样

if(strtotime($givendate) > strtotime('3/21/xxxx') && strtotime($givendata) < strtotime('4/19/xxxx')) {
   // Its within range
}
于 2012-04-26T07:44:15.537 回答
0

尝试这个

 if (strtotime($udate) >= strtotime($realStartDate) && strtotime($udate) <= strtotime($realEndDate) ) {
    echo 'within tange';
}
else{
    echo 'Not in range';
}
于 2012-04-26T09:48:52.190 回答