php - Check if something is between two values?

643

How do I go about checking to see if the current date is between two other dates?

I've been trying different operators, but haven't been able to figure this out.

So, if I have afrom date of2/2/2010 and ato date of2/10/2010, how can I returnTRUE if the current date (2/4/2010) falls between those two dates?

851

Answer

Solution:

Off the top of my head, I don't know of a date comparison operator in PHP, but I would usestrtotime() on all three dates, then do simple mathematical comparisons.

<?php

$early_date = strtotime("02/02/2010");
$date = strtotime("02/04/2010");
$late_date = strtotime("02/10/2010");

if (($early_date < $date) && ($date < $late_date)) {
  echo "true";
}

returns true.

702

Answer

Solution:

To do a comparison like this you need to do separate comparisons. If $d is the date you want to compare, $d1 is the earlier date, and $d2 is the later date, it would be something like:

if ((strtotime($d) > strtotime($d1)) and (strtotime($d) < strtotime($d2))) {
    return true;
} else {
    return false;
}
777

Answer

Solution:

If past date one and before date two, then it's between them.

544

Answer

Solution:

the googles told me

if ( strtotime($date) > strtotime('22/09/2008') && strtotime($date) < strtotime('28/09/2008'))

http://answers.yahoo.com/question/index?qid=20081003113922AAHnQsp

761

Answer

Solution:

wouldn't this work ?

 ( ($lowerlimitdate <= $checkingdate) && ($checkingdate <= $upperlimitdate))

People are also looking for solutions to the problem: php - Why does phpDoc opening sequence have two stars in `/**` instead of just `/*`

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.