0

I have two dates from-date & to-date.

I have to compare them from existing dates shown below, whether any of the day fall between them or not using php?
i can do for single date checking ,but i am confuse for the two date checking.

Example: i have to check these dates:-> from=15 March 2013 & 15 April 2013 between the following dates whether any days falls in between these two date or not.

following data from db table

 #       from date            to-date
-----------------------------------------
1     01 April 2013         30 April 2013   //here we will find as falling
2     01 May 2013           15 May 2013   
3     01 June 2013          20 June 2013

Currently,in my mind not even a single logic is coming to try. Please give me any logic or suggestions regarding this issue..

Dan
  • 2,086
  • 11
  • 71
  • 137
  • Do you want to check if a date is in between two dates? – Vishnu R Apr 04 '13 at 06:26
  • Yes Exactly. I have mention the same in my question. Its like i have to set my Amount Budget between two dates, if I have already set budget from newly type from-date & to-date. Then i have to show error message – Dan Apr 04 '13 at 06:28
  • check the Datetime class , did you ? there is a lot of methods one can use to compare dates. – mpm Apr 04 '13 at 06:33
  • @mpm first time hearing about php Datetime class . i am checking it now....This is my first project in php – Dan Apr 04 '13 at 06:35
  • Are you getting the results from database? If yes then check this my `sql query`. – Dead Man Apr 04 '13 at 06:37
  • @DeadMan Sorry! Please read my question once more. – Dan Apr 04 '13 at 06:38
  • Do you want to check if the _period_ 15 Match 2013 _to_ 15 April 2013 falls inbetween the three periods in the db table? – quad16 Apr 04 '13 at 06:57
  • @Pärserk Yes, exactly, now you know my actual doubt! – Dan Apr 04 '13 at 06:59

2 Answers2

3

The simplest way to compare dates is to convert them to a unix timestamp

Because the unix timestamp is an integer, you can simply use relational operators to compare them.

Example

// set some example data
$referenceDate = '01 April 2013';
$fromDate = '01 January 2013';
$toDate = '01 June 2013';

// convert dates to timestamps (strings to integers)
$referenceTimestamp = strtotime( $referenceDate );
$fromTimestamp = strtotime( $fromDate );
$toTimestamp = strtotime( $toDate );

// isBetween is Boolean TRUE if reference date is greater or equal fromDate and smaller or equal toDate
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;

EDIT 1

To actually answer your question:

You have two ranges you need to test for overlap, this question has been answered here What's the most efficient way to test two integer ranges for overlap?

// our two ranges overlap if the following conditions are met
$dateRangeOverlaps = $referenceFromTimestamp <= $toTimestamp and $fromTimestamp <= $referenceToTimestamp;
Community
  • 1
  • 1
Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
  • can you write comment, and explain it in little details, because am unable to understand your code. And why you are using `$referenceDate = '01 April 2013';`? – Dan Apr 04 '13 at 06:54
  • you are very talented. You are answering me in very standard and in short cut way. I am unable to understand your code. Is it possible for you to make your answer simple? – Dan Apr 04 '13 at 06:58
  • Don't worry, you are most likely not less talented, it's just a matter of training and experience. I've added comments to each step, also read the answer I have linked. – Michel Feldheim Apr 04 '13 at 07:04
  • But you are checking only one date `$referenceDate = '01 April 2013';` How can i check for my above posted date rows in between the two dates? Please help – Dan Apr 04 '13 at 07:31
  • That's answered below EDIT1. – Michel Feldheim Apr 04 '13 at 08:29
  • I tried to solve, but still have small problem. I posted question here http://stackoverflow.com/questions/15871992/php-unable-to-compare-two-dates-result?noredirect=1#comment22594984_15871992. I think you can only help me – Dan Apr 08 '13 at 06:18
-1

Please try the code,

    $ourdate = strtotime('1 April 2013'); // Your date which is to be checked

    $from = strtotime('15 March 2013'); // From date
    $to = strtotime('15 April 2013'); // To date

    if ($ourdate >= $from && $ourdate <= $to)
    {
      echo "Date falls";
    }
    else
    {
    echo "No Date falls";
    }

If you need to check several dates, pass it as an array, like below...

   $i=0;
   $dates= array("11 April 2013","16 April 2013");
   foreach($dates as $ourdates)
   {
    $ourdate= strtotime($ourdates); //Your dates to be checked
    $from = strtotime('15 March 2013'); // From date
    $to = strtotime('15 April 2013'); // To date

          if ($ourdate >= $from && $ourdate <= $to)
              {
                 $i++;
              }
    }
    if($i>0)
    {
    echo "Date falls";
    }
    else
    {
    echo "No Date falls";
    }
Vishnu R
  • 1,859
  • 3
  • 26
  • 45
  • i need to check between two date, you are checking only one date. i have to check these dates:-> from=15 March 2013 & 15 April 2013 between the above shown date data. If any of the day falls between these dates from my above shown data then i have to give error messsage – Dan Apr 04 '13 at 06:46
  • I edited the answer. If you are about to check for multiple dates, pass the dates as an array, like I did above.. – Vishnu R Apr 04 '13 at 06:47
  • i think you are not understanding my question doubt. Kindly read my question once again.. – Dan Apr 04 '13 at 06:51