php - 添加 + 7 天到日期格式 mm dd, YYYY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5174789/
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
php - add + 7 days to date format mm dd, YYYY
提问by Michael
I have date of this format March 3, 2011 in database and I need to extend it with 7 days. I mean
我在数据库中有这种格式的日期 2011 年 3 月 3 日,我需要将其延长 7 天。我的意思是
$date = $date + 7. 是否有任何内置功能可以做到这一点?
回答by Matthew Scharley
$date = "Mar 03, 2011";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
回答by Kenshi Mokak
The "+1 month" issue with strtotime
strtotime 的“+1 个月”问题
As noted in several blogs, strtotime() solves the "+1 month" ("next month") issue on days that do not exist in the subsequent month differently than other implementations like for example MySQL.
正如几篇博客中所指出的,strtotime() 在下个月不存在的日子解决了“+1 个月”(“下个月”)问题,这与其他实现(例如 MySQL)不同。
$dt = date("Y-m-d");
echo date( "Y-m-d", strtotime( "$dt +1 day" ) ); // PHP: 2009-03-04
echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP: 2009-03-31
回答by Nicolas
Another more recent and object style way to do it :
另一种最近的对象风格的方法来做到这一点:
$date = new DateTime('now');
$date->add(new DateInterval('P7D'));
回答by IlludiumPu36
echo date('d/m/Y', strtotime('+7 days'));
echo date('d/m/Y', strtotime('+7 days'));
回答by Jan Dragsbaek
yes
是的
$oneweekfromnow = strtotime("+1 week", strtotime("<date-from-db>"));
on another note, why do you have your date in the database like that?
另一方面,为什么你的日期在数据库中?
回答by user11528018
onClose: function(selectedDate) {
$("#dpTodate").datepicker("option", "minDate", selectedDate);
var maxDate = new Date(selectedDate);
maxDate.setDate(maxDate.getDate() + 6); //6 days extra in from date
$("#dpTodate").datepicker("option", "maxDate", maxDate);
}
回答by Vadim Samokhin
I would solve this like that. First, I'd create an instance of your given datetime object. Then, I'd create another datetime object which is 7 days later than the initial one. And finally, I'd format it the way you like.
我会这样解决这个问题。首先,我会创建一个给定日期时间对象的实例。然后,我将创建另一个日期时间对象,它比初始对象晚 7 天。最后,我会按照您喜欢的方式对其进行格式化。
With meringue library, this is quite intuitive and elegant. Here's the code:
使用蛋白酥皮库,这是非常直观和优雅的。这是代码:
(new Future(
new FromCustomFormat('F j, Y', 'March 3, 2011'),
new NDays(7)
))
->value();
The result is a string in ISO8601 format. If you like, you can format it anyway you like using the same ISO8601 syntax:
结果是 ISO8601 格式的字符串。如果您愿意,可以使用相同的 ISO8601 语法以任何方式对其进行格式化:
(new ISO8601Formatted(
new Future(
new FromCustomFormat('F j, Y', 'March 3, 2011'),
new NDays(7)
),
'F j, Y'
))
->value();
The code above uses meringue library. Here's a quick start, you can take a look if you want.
上面的代码使用蛋白酥皮库。这是一个快速入门,如果您愿意,可以查看一下。