在PHP中向$ Date添加天数


215

我有以下形式的日期作为MySQL查询的一部分返回 2010-09-17

我想将变量$ Date2设置为$ Date5,如下所示:

$Date2 = $Date + 1

$Date3 = $Date + 2

等等..

以便它返回2010-09-182010-09-19等等。

我努力了

date('Y-m-d', strtotime($Date. ' + 1 day'))

但这给了我约会之前$Date

什么是以“ Ymd”格式获取日期的正确方法,以便它们可以在其他查​​询中使用?


Answers:


446

您要做的就是使用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'));
?>

并正确输出:

2010-09-18
2010-09-19

7
它也可以正确输出day...- date("Y-m-d", strtotime('2010-09-17 + 1 day'))> 2010-09-18date("Y-m-d", strtotime('2010-09-17 + 2 day'))->2010-09-19
Daniel Vandersluis,2010年

只是尝试这种方式,它的工作。我仍然不知道为什么我在开始尝试的第一天就获得了价值。
伊斯塔里2010年

如果我要添加一个天数变量怎么办?
卡洛斯·马丁斯

3
@Carlos echo date(“ Ymd”,strtotime($ Date。'+'。$ variable。'days'));
Pradeep Kumar Prabaharan

不,这种方法根本不起作用。它返回的日期是2004
。– gegobyte '16

81

如果您使用的是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');

查看DateInterval构造函数手册页,以了解如何构造其他添加到日期的句点(2天为'P2D',3 天为'P3D',依此类推)。

如果没有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"

抱歉,丹尼尔(Daniel),但网络服务器运行php 5.2.6,因此无法正常工作
Istari 2010年

2
DateTime::construct使用与strtotime解析日期相同的机制,因此您也可以这样做new DateTime("+1 day $date"),不需要5.3
Gordon

我不是要拒绝投票,而是以某种方式做到了= /
Steven

这是php 5.5+的实际答案
TheLegendaryCopyCoder

29

从PHP 5.2起,您可以对DateTime对象使用Modify:

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');

添加月份...(在较小程度上,几年)时要小心


接受的答案只会给出15000+天的答案,而当您添加25000+天时,此答案也会给出答案。–
Pradeep Kumar Prabaharan

您也可以在DateTimeImmutable()上查询此内容,并获得修改后的副本,而无需更改原始副本。
哈维·蒙特罗

17

以下是演示日期修改的小片段:

$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";

你能看到我相关的(问题)[ stackoverflow.com/q/61421783/8919244]吗?
费萨尔

8

您也可以使用以下格式

strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));

您可以通过以下方式堆叠更改:

strtotime("+1 day", strtotime("+1 year", strtotime($date)));

请注意,此方法与其他答案中的方法之间的区别:无需串联值+1 dayand <timestamp>,您只需将时间戳作为的第二个参数传递即可strtotime



5

这是您查询的最简单解决方案

$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

2

这里有一个简单的方法来解决这个问题。

<?php
   $date = "2015-11-17";
   echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>

输出将是:

2015-11-22

从这里找到解决方案- 如何在PHP中添加日期


0

所有人都必须使用以下代码:

$nday = time() + ( 24 * 60 * 60);    
echo 'Now:       '. date('Y-m-d') ."\n";    
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";

1
您不应该这样操作,最终您会遇到leap秒问题
内森(Nathan)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.