php 日期格式 YYYY-MM-DD 从现在起减去或添加一周?

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

php date format YYYY-MM-DD minus or add one week from now?

php

提问by Adam Ramadhan

today 22-05-2011 so it should be 29-05-2011? ( plus 1 week ) 
or
today 22-05-2011 so it should be 15-05-2011? ( minus 1 week ) 

thanks for looking in.

感谢您的关注。

Adam Ramadhan

亚当·拉马丹

回答by JohnP

Use strtotime()

使用strtotime()

echo date('d-m-Y', strtotime("+1 week")); //1 week in the future
echo date('d-m-Y', strtotime("-1 week")); //1 week ago

回答by Luká? Lalinsky

You can use the DateTimeclass to do calendar calculations. For exaple, to add one week, you could use code like this:

您可以使用DateTime该类进行日历计算。例如,要添加一周,您可以使用如下代码:

$date = new DateTime('22-05-2011');
$date->modify('+1 week');

回答by John Green

strtotimewill handle this.

strtotime会处理这个问题。

$pDate = strtotime('22-05-2011 + 1 week');
echo date('d-m-Y',$pDate);


Added: This is if you want to start from a specific date. If you just want 'today' +/- a week', mark JohnP's answer as correct. : )

补充:这是如果您想从特定日期开始。如果您只想要“今天”+/- 一周”,请将 JohnP 的答案标记为正确。:)

回答by swathi_sri

In case If you dint want to hard code today's date you can use Carbon class methods of php.

如果你不想硬编码今天的日期,你可以使用 php 的 Carbon 类方法。

Carbon::now()->subWeek(1);
Carbon::now()->addWeek(1);