0

I want to know how to detect if a date range matched specified condition:

Expected results:

<?php
$start_date1 = '2016-05-06 00:00:00';
$start_date2 = '2016-01-06 00:00:00';

$result1 = is_date_range_exceeds_3_months($start_date1);
$result2 = is_date_range_exceeds_3_months($start_date2);
//lets say 'now' is '2016-06-06 00:00:00'

//Expected result of $result1 = false
//Expected result of $result2 = true
?>

(Please make correction to my question as I think this question is not in correct format/words)

Thanks!

mokalovesoulmate
  • 271
  • 2
  • 6
  • 18

2 Answers2

0

You could use DateTime for this.

function is_date_range_exceeds_3_months($strDate)
{

    $userDate = new \DateTime($strDate); // @todo: Check if is valid
    $checkDate = new \DateTime(); // By default date seed is now
    $checkDate->modify('+3 months'); // Set period

    if($userDate > $checkDate) {
        return true;
    } else {
        return false;
    }

}

This is just a tip, sorry if it contains some typos.

Edit by OP:

function is_date_range_exceeds_x_months($month_limiter, $start_date) {
    $userDate = new DateTime($start_date); // @todo: Check if is valid
    $userDate = $userDate->format('Y-m-d H:i:s');

    $checkDate = new DateTime(); // By default date seed is now
    $checkDate->modify('-' . $month_limiter . ' months'); // Set period
    $checkDate = $checkDate->format('Y-m-d H:i:s');


    if ($userDate < $checkDate) {
        return true;
    } else {
        return false;
    }
}
mokalovesoulmate
  • 271
  • 2
  • 6
  • 18
Muriano
  • 383
  • 5
  • 13
0
<?php
$start_date1 = '2016-05-06 00:00:00';
$start_date2 = '2016-01-06 00:00:00';

$result1 = is_date_range_exceeds_3_months($start_date1);
$result2 = is_date_range_exceeds_3_months($start_date2);
//lets say 'now' is '2016-06-06 00:00:00'

function is_date_range_exceeds_3_months($start_date)
{
    $lastdates=date('Y-m-d H:i:s', strtotime('-3 month'));
    $current_dates=date("Y-m-d H:i:s");
   echo 'currentdate'.$current_dates.'<br/>';
   echo 'Lasytdate'.$lastdates.'<br/>';

    if (($start_date > $lastdates) && ($start_date < $current_dates))
    {
      return true;
    }
    else
    {
      return false;
    }   

}
//Expected result of $result1 = false
//Expected result of $result2 = true
?>
Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52