find if date is older than 30 days

前端 未结 5 1619
我寻月下人不归
我寻月下人不归 2020-12-23 09:01

The date string looks like this

2011-08-19 17:14:40

(year-month-day hours:minutes:seconds)

How can I find out if the date is older than

相关标签:
5条回答
  • 2020-12-23 09:20

    If you are on PHP 5.3 or higher you can do:

    $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.';
    }
    
    0 讨论(0)
  • 2020-12-23 09:35

    Try using something like this:

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

    Besides, this string looks like it is stored in SQL as datetime/timestamp field. You can directly select all entries from your database with old dates using:

    SELECT ... WHERE `datetime` + INTERVAL 30 DAY < NOW()
    
    0 讨论(0)
  • 2020-12-23 09:37

    You can use Carbon as follows

    if (30 - ((new \Carbon\Carbon($given_date, 'UTC'))->diffInDays()) < 0) {
        echo "The date is older than 30 days";
    }
    
    0 讨论(0)
  • 2020-12-23 09:43
    strtotime('2011-08-19 17:14:40') + 30 * 24 * 60 * 60 < time();
    
    0 讨论(0)
  • 2020-12-23 09:43

    With the meringue library, this can be done in at least two ways.

    The first one looks like the following:

    (new Future(
        new DateTimeParsedFromISO8601String('2011-08-19 17:14:40'),
        new NDays(30)
    ))
        ->earlierThan(
            new Now()
        );
    

    The semantics is the following: first, you parse a date from an ISO8601 string, then create a future date which is thirty days later than that, and finally compare it with current datetime, that is, now.

    The second way is creating an interval from a datetime range and counting the days it consists of. It looks like that:

    (new TotalFullDays(
        new FromRange(
            new FromISO8601('2011-08-19 17:14:40'),
            new Now()
        )
    ))
        ->value();
    

    Both approaches are quite intuitive and don't make you remember special php datetime expressions. Instead, every implementation is autocompleted; you just need to build a correct object that suits your needs.

    0 讨论(0)
提交回复
热议问题