0

I have been trying my best to get it done but no luck so far . I have seen this post where they are just using dates only . But I would like to use date with time . In first case:-

Method: 1

       public static function dateIsInBetween(\DateTime $from, \DateTime $to, \DateTime $subject)
    {
        return $subject->getTimestamp() > $from->getTimestamp() && $subject->getTimestamp() <  $to->getTimestamp() ? true : false;
    }
        $checkDate = new \DateTime("2020-01-23 13:42:49");
        $DateBegin = new \DateTime("2020-01-21 11:00:00");
        $DateEnd   = new \DateTime("2020-01-25 13:00:00");

        echo self::dateIsInBetween($DateBegin, $DateEnd, $checkDate) ? "is between" : "NO GO!";
        //Wrong Result :- is between 

Method: 2

 $checkDate = strtotime("2020-01-23 13:42:49");
        $startDate = strtotime("2020-01-21 11:00:00");
        $endDate = strtotime("2020-01-25 13:00:00");
        if($checkDate > $startDate && $checkDate < $endDate) {
           echo 'IN BETWEEN';

        }else{
           echo 'NO';
           }

        //Wrong Result :- is between 
  • Your second example should presumably be just `$checkDate = strtotime("2020-01-23 13:42:49"));`, not ` $checkDate = strtotime(date("2020-01-23 13:42:49"));` Even without that, though, this is working correctly (after fixing the syntax error from the missing close parenthesis on the `$endDate` line). Jan 23 is indeed between Jan 21 and 25, and that's the result this gives. – Greg Schmidt May 03 '20 at 21:42
  • I have removed it and still giving the same result @GregSchmidt – Yii2Developer May 03 '20 at 21:50
  • Running the code that you're currently showing here as Method 2 gives you a result of "NO"? – Greg Schmidt May 03 '20 at 21:56
  • @GregSchmidt Here we go for online php [link](https://paiza.io/projects/md3onccdMrrw7xk56bCS6A?language=php) – Yii2Developer May 03 '20 at 22:01
  • 1
    That says "IN BETWEEN". Which is surely should. Unless you think that Jan 23 is not in fact between Jan 21 and Jan 25? – Greg Schmidt May 03 '20 at 22:06
  • @GregSchmidt could you read my question as i am not only checking for the date but also for the time mate . And time is not between startTime `11:00:00` endTime `13:00:00` and the time i would like to check `13:42:49` – Yii2Developer May 04 '20 at 04:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213060/discussion-between-iamnareshsaini-and-greg-schmidt). – Yii2Developer May 04 '20 at 04:39

1 Answers1

0

You can check times by using below code and for dates you can follow above link mentioned in the question.

$startTime = strtotime("7:00");
$endTime   = strtotime("10:30");

$chkStartTime = strtotime("10:00");
$chkEndTime   = strtotime("12:10");

if($chkStartTime > $startTime && $chkEndTime < $endTime)
{   #-> Check time is in between start and end time
    echo "1 Time is in between start and end time";
}elseif(($chkStartTime > $startTime && $chkStartTime < $endTime) || ($chkEndTime > $startTime && $chkEndTime < $endTime))
{   #-> Check start or end time is in between start and end time
    echo "2 ChK start or end Time is in between start and end time";
}elseif($chkStartTime==$startTime || $chkEndTime==$endTime)
{   #-> Check start or end time is at the border of start and end time
    echo "3 ChK start or end Time is at the border of start and end time";
}elseif($startTime > $chkStartTime && $endTime < $chkEndTime)
{   #-> start and end time is in between  the check start and end time.
    echo "4 start and end Time is overlapping  chk start and end time";
}