Search code examples
phpmysqlcodeigniter-2expressionengine

Where date is later than


I am storing a date as 'd-m-Y H:i:s'.

I need to check if this stored date is later than the $limit_date variable. The $limit_date variable is -30 days of the current date, stored in 'd-m-Y'.

I had thought the following would work, but no results are returned.

$limit_date = strtotime('-30 day');
$limit_date = date("d-m-Y", $limit_date);

$this->db->where('date_submitted >', $limit_date);

My test date_submitted is '25-04-2017 09:13:13' with $limit_date returning '26-03-2017'.


Solution

  • In this code we are using DateTime and DateInterval. DateInterval for subtracting an exact amount of days you want to.

    Try this code snippet here

    <?php
    ini_set('display_errors', 1);
    $days=30;
    $object = new DateTime("25-04-2017 09:13:13");//giving the date from which we want to subtract.
    $object->sub(new DateInterval("P".$days."D"));//passing the interval which we want to subtract
    echo $object->format("d-m-Y H:i:s");