-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?

Community
  • 1
  • 1
spsingh
  • 19
  • 3
  • Did you do your duty and searched? http://stackoverflow.com/questions/976669/how-to-check-if-a-date-is-in-a-given-range – Shegit Brahm Apr 26 '12 at 07:43
  • did you research on mysql between or this link: http://www.daniweb.com/web-development/databases/mysql/threads/53025/mysql-select-rows-in-a-date-range – SuperNoob Apr 26 '12 at 07:58
  • Why are you converting the strings to a timestamp and then back to the exact same string? – JJJ Apr 26 '12 at 09:46
  • select all the dates or your range and store them in array then use php inarray function –  Apr 26 '12 at 07:48
  • Is `2 March` before or after `11 March`? According to your code... it's after. Compare timestamps and not human formats. – Niet the Dark Absol Apr 26 '12 at 16:22

5 Answers5

2

try this one its working......

<?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';
        }
?>
Chintan
  • 1,204
  • 1
  • 8
  • 22
1

Compare timestamps not the string representations!

if(strtotime($apple) < strtotime($endDate) && strtotime($apple) > strtotime($startDate)){
 // All ok!
}
Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
0

Like this

if(strtotime($givendate) > strtotime('3/21/xxxx') && strtotime($givendata) < strtotime('4/19/xxxx')) {
   // Its within range
}
Starx
  • 77,474
  • 47
  • 185
  • 261
0

You can use 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 
}

Putting it in a function if format is (1 july)

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;

}
Baba
  • 94,024
  • 28
  • 166
  • 217
0

try this

 if (strtotime($udate) >= strtotime($realStartDate) && strtotime($udate) <= strtotime($realEndDate) ) {
    echo 'within tange';
}
else{
    echo 'Not in range';
}
Nauphal
  • 6,194
  • 4
  • 27
  • 43