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
.
但是这给了我$ 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?
以“Y-m-d”格式获取日期的正确方法是什么,以便在另一个查询中使用它们?
9 个解决方案
#1
294
All you have to do is use days
instead of day
like this:
你所要做的就是用几天代替这一天:
<?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
#2
57
If you're using PHP 5.3, you can use a DateTime
object and its add
method:
如果使用的是PHP 5.3,可以使用DateTime对象及其添加方法:
$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
constructor manual 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"
#3
15
From PHP 5.2 on you can use modify with a DateTime object:
从PHP 5.2开始,您可以使用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)
增加月份时要小心……(在更小的程度上,是几年)
#4
13
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";
#5
7
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
.
注意这种方法与其他答案中的方法的区别:您可以将时间戳作为strtotime的第二个参数传入,而不是将值+1 day和
#6
2
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
#7
1
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
#8
1
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中添加日期
#9
0
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";
#1
294
All you have to do is use days
instead of day
like this:
你所要做的就是用几天代替这一天:
<?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
#2
57
If you're using PHP 5.3, you can use a DateTime
object and its add
method:
如果使用的是PHP 5.3,可以使用DateTime对象及其添加方法:
$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
constructor manual 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"
#3
15
From PHP 5.2 on you can use modify with a DateTime object:
从PHP 5.2开始,您可以使用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)
增加月份时要小心……(在更小的程度上,是几年)
#4
13
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";
#5
7
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
.
注意这种方法与其他答案中的方法的区别:您可以将时间戳作为strtotime的第二个参数传入,而不是将值+1 day和
#6
2
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
#7
1
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
#8
1
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中添加日期
#9
0
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";