PHP 日期函数 7 天前

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

PHP Date Function Seven days previous

phpdate

提问by Harsha M V

I am trying to use PHP's Date Function to get the date of 7 days earlier in YYYY-MM-DD format.

我正在尝试使用 PHP 的日期函数以 YYYY-MM-DD 格式获取 7 天前的日期。

date('Y-m-d');

when i try

当我尝试

date('Y-m-d-7');

i get an error

我得到一个错误

回答by Gavin

Use the strtotimemethod provided by PHP.

使用strtotimePHP提供的方法。

date('Y-m-d', strtotime('-7 days'))

date('Y-m-d', strtotime('-7 days'))

Thanks to @lonesomeday for pointing out my mistake in the comments ;)

感谢@lonesomeday 在评论中指出我的错误;)

回答by lonesomeday

With this, as with all PHP date stuff, it's nicer to use the DateTimeclass.

有了这个,与所有 PHP 日期内容一样,使用DateTime该类会更好。

$date = new DateTime('7 days ago');
echo $date->format('Y-m-d');