mokalovesoulmate mokalovesoulmate - 5 months ago 6
PHP Question

PHP detect if between specified date and now() matched specified condition

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!

Answer

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.

Comments