php 查找日期是否早于 30 天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7130738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find if date is older than 30 days
提问by Ella
The date string looks like this
日期字符串看起来像这样
2011-08-19 17:14:40
2011-08-19 17:14:40
(year-month-day hours:minutes:seconds)
(年-月-日时:分:秒)
How can I find out if the date is older than the current date with more than 30 days?
如何确定日期是否比当前日期早 30 天?
回答by RiaD
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:
此外,这个字符串看起来像是作为日期时间/时间戳字段存储在 SQL 中。您可以直接使用旧日期从数据库中选择所有条目:
SELECT ... WHERE `datetime` + INTERVAL 30 DAY < NOW()
回答by Collin Krawll
If you are on PHP 5.3 or higher you can do:
如果您使用的是 PHP 5.3 或更高版本,则可以执行以下操作:
$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.';
}
回答by Shriganesh Shintre
You can use Carbon as follows
您可以按如下方式使用碳
if (30 - ((new \Carbon\Carbon($given_date, 'UTC'))->diffInDays()) < 0) {
echo "The date is older than 30 days";
}
回答by gion_13
strtotime('2011-08-19 17:14:40') + 30 * 24 * 60 * 60 < time();
回答by Vadim Samokhin
With the meringuelibrary, 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
.
语义如下:首先,您从 ISO8601 字符串解析日期,然后创建一个比该日期晚三十天的未来日期,最后将其与当前日期时间进行比较,即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.
这两种方法都非常直观,不会让您记住特殊的 php 日期时间表达式。相反,每个实现都是自动完成的;您只需要构建一个适合您需求的正确对象。