0

I have to check if the incoming date is between 3 and 6 months before today. If it is outside this range, it has to execute certain code.

below is the code

<?php

$date1 = '22-10-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3))){
    echo "Inside Range";
}else echo "Out of Range";

?>

For example if

  1. Incoming date is 20-02-2018 - Out of Range.
  2. Incoming date is 20-10-2017 - Inside Range.
  3. Incoming date is 20-08-2017 - Out of Range.
Mike
  • 23,542
  • 14
  • 76
  • 87
hemanth kumar
  • 529
  • 1
  • 6
  • 25

2 Answers2

7

You are checking with || in your case you need to use && because you need date BETWEEN

$date1 = '20-08-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
    echo "Inside Range";
}else { 
   echo "Out of Range";
}

Explanation: Need to change your condition from if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3))) to if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){

It's also significantly easier if you're using DateTime objects:

$date1 = new DateTime('20-08-2017');
$date2 = new DateTime('-3 months');
$date3 = new DateTime('-6 months');

if($date1 < $date2 && $date1 > $date3) {
    echo "Inside Range";
} else {
    echo "Out of Range";
}
Mike
  • 23,542
  • 14
  • 76
  • 87
Krish
  • 387
  • 2
  • 13
  • best to place that `else` be in braces. I know the OP didn't, but they should and it should be corrected. – Funk Forty Niner Feb 22 '18 at 17:45
  • It's also significantly easier if you're using DateTime objects. Mind if I hijack your answer to add that in? – Mike Feb 22 '18 at 17:48
  • you are welcome @Mike , i just tried to solve hemanth's problem with his own code... but providing best approach is always welcome – Krish Feb 22 '18 at 17:51
  • Adding to it would be good; only hope the answer won't be overwritten though. – Funk Forty Niner Feb 22 '18 at 17:52
  • @FunkFortyNiner I wouldn't do that... :) – Mike Feb 22 '18 at 17:55
  • I didn't think you would @Mike - that's why I didn't ping you in that comment. I was going to, but then said (*Nah...*), he knows what he's doing ;-) – Funk Forty Niner Feb 22 '18 at 17:56
  • Thank you @Mike. That was of great help. – hemanth kumar Feb 22 '18 at 17:58
  • @FunkFortyNiner I was working on my own answer when this one was posted that was basically the same as it using `DateTime` which didn't really warrant posting another answer, so I thought "why let this quality 7 lines of code just go to waste?" – Mike Feb 22 '18 at 17:59
  • Hey @Mike hope things are still rockin' over your way. I'll get that (apparently amazing) cup of coffee from you somehow/day ;-) *cheers* (Krish: pardon my bit of hijacking here). ...and as I was typing this out while you were also,...... I was going to say that too, in asking why you weren't posting an answer of your own.... oh well.. you got a little something a couple of minutes ago ;-) Gee... wonder what huh? – Funk Forty Niner Feb 22 '18 at 18:02
  • @FunkFortyNiner Yeah, it was this one https://stackoverflow.com/questions/48776512/why-this-result-in-php/48776593#48776593. Weird how stupid answers like that end up getting so many upvotes sometimes... – Mike Feb 22 '18 at 18:08
  • @Mike hey, every little bit helps! everybody's happy in the end ;-) – Funk Forty Niner Feb 22 '18 at 18:40
0

You can do like this:

$today=date_create(date("Y-m-d"));
$date=date_create("2018-06-12");
$diff=date_diff($today,$date)->format("%a");

if ($diff > 90 && $diff < 180) {
    echo "Inside range";
}
else {
    echo "Out of range";
}
  • 1
    You have the wrong variable names in your `date_diff` function and you also cannot convert a `DateInterval` to Int. You need to `format()` it. See http://php.net/manual/en/datetime.diff.php – Mike Feb 22 '18 at 17:53