php 检查日期是否在当前时间的 7 天内

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14370018/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:08:54  来源:igfitidea点击:

Checking if a date is within 7 days of the current time

phpmysqldate

提问by David G

So I have a php function which interacts with a database and pulls data out. However I have a date stamp (in this case an upload date). I want to make it so that if something has been uploaded within the last 7 days, it posts that to users to make them aware of it being 'new'.

所以我有一个与数据库交互并提取数据的 php 函数。但是我有一个日期戳(在这种情况下是上传日期)。我想这样做,如果在过去 7 天内上传了某些内容,它会将其发布给用户,让他们知道它是“新的”。

So far I have this:

到目前为止,我有这个:

if( strtotime($rows['Date']) > strtotime('now') ) {
    echo '<div class="l-new"><a href="#"><!-- --></a></div>';
}

The $rows thing is taken care of and works from some other code above this line. But this is the line I am concerned with.

$rows 事情由该行上方的其他一些代码处理和工作。但这是我关心的线。

I want to say "if the date in the database is older than the current time, and is less than 7 days old, do the following { //echo the stuff about it being new }

我想说“如果数据库中的日期比当前时间早,并且不到 7 天,请执行以下操作 { //echo the stuff about it is new }

Problem is, how do I phrase that if statement to make it within 7 days? Absolutely cannot think of how unfortunately. I'm sure it is quite simple! Thank you

问题是,如何在 7 天内表达 if 语句?绝对想不到有多不幸。我相信这很简单!谢谢

回答by Jim

if( strtotime($rows['Date']) > strtotime('-7 day') ) {
    echo '<div class="l-new"><a href="#"><!-- --></a></div>';
}

The strtotime('-7 day')part returns the time 7 days ago.

strtotime('-7 day')部分返回时间为7天前。