find if date is older than 30 days

前端 未结 5 1618
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题