find if date is older than 30 days

find if date is older than 30 days

Asked on December 27, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Have a look at the code,

    if(strtotime('2011-08-19 17:14:40') < strtotime('-30 days')) {
        // this is true
    }
    

    The string is stored in SQL as datetime/timestamp field. But by using below code one can directly select and get all entries in the database with old dates . here is the example code,

    SELECT ... WHERE `datetime` + INTERVAL 30 DAY < NOW()
    
    Answered on December 27, 2018.
    Add Comment

    For the php 5.3 or above version users,

    $someDate = new \DateTime('2011-08-19 17:14:40');
    $now = new \DateTime();
     
    if($someDate->diff($now)->days > 30) {
      echo 'The date was more than 30 days ago.';
    }
    
    Answered on December 27, 2018.
    Add Comment

    This will be very simple method when one use carbon in the code,

    if (30 - ((new \Carbon\Carbon($given_date, 'UTC'))->diffInDays()) < 0) {
      echo "The date is older than 30 days";
    }
    
    Answered on December 27, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.