在 PHP 中为 $Date 添加天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3727615/
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
Adding days to $Date in PHP
提问by Istari
I have a date returned as part of a mySQL query in the form 2010-09-17
我在表单中作为 mySQL 查询的一部分返回了一个日期 2010-09-17
I would like to set the variables $Date2 to $Date5 as follows:
我想将变量 $Date2 设置为 $Date5 如下:
$Date2 = $Date + 1
$Date2 = $Date + 1
$Date3 = $Date + 2
$Date3 = $Date + 2
etc..
等等..
so that it returns 2010-09-18
, 2010-09-19
etc...
所以它返回2010-09-18
,2010-09-19
等...
I have tried
我试过了
date('Y-m-d', strtotime($Date. ' + 1 day'))
but this gives me the date BEFORE $Date
.
但这给了我 BEFORE 的日期$Date
。
What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?
以“Ymd”格式获取我的日期以便它们可以在另一个查询中使用的正确方法是什么?
回答by shamittomar
All you have to do is use days
instead of day
like this:
你所要做的就是使用days
而不是day
这样:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
And it outputs correctly:
它正确输出:
2010-09-18
2010-09-19
回答by Daniel Vandersluis
If you're using PHP 5.3, you can use a DateTime
object and its add
method:
如果您使用的是 PHP 5.3,则可以使用DateTime
对象及其add
方法:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Take a look at the DateInterval
constructormanual page to see how to construct other periods to add to your date (2 days would be 'P2D'
, 3 would be 'P3D'
, and so on).
查看DateInterval
构造函数手册页,了解如何构造其他时间段以添加到您的日期(2 天是'P2D'
,3天是'P3D'
,依此类推)。
Without PHP 5.3, you should be able to use strtotime
the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):
如果没有 PHP 5.3,你应该能够使用strtotime
你的方式(我已经测试过它并且它在 5.1.6 和 5.2.10 中都有效):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
回答by Omn
From PHP 5.2 on you can use modify with a DateTime object:
从 PHP 5.2 开始,您可以将 modify 与 DateTime 对象一起使用:
http://php.net/manual/en/datetime.modify.php
http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Be careful when adding months... (and to a lesser extent, years)
添加月份时要小心......(在较小程度上,年份)
回答by Madhu V Rao
Here is a small snippet to demonstrate the date modifications:
这是一个演示日期修改的小片段:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
回答by SztupY
You can also use the following format
您也可以使用以下格式
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
You can stack changes this way:
您可以通过这种方式堆叠更改:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day
and <timestamp>
, you can just pass in the timestamp as the second parameter of strtotime
.
请注意这种方法与其他答案中的方法之间的区别:您可以将时间戳作为 的第二个参数传入,而不是将值+1 day
和连接起来。<timestamp>
strtotime
回答by Engr Atiq
Here is the simplest solution to your query
这是您查询的最简单解决方案
$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days
echo date_format($date,"Y-m-d"); //set date format of the result
回答by Carlos Martins
Using a variable for Number of days
使用天数变量
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.'days');
echo newDate('d/m/Y', $soma); //format new date
回答by JoyGuru
Here has an easy way to solve this.
这里有一个简单的方法来解决这个问题。
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
Output will be:
输出将是:
2015-11-22
Solution has found from here - How to Add Days to Date in PHP
解决方案从这里找到 -如何在 PHP 中添加天数
回答by vikasgore
All have to use bellow code:
都必须使用波纹管代码:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";