在PHP中将日期加三个月

110

我有一个变量名为 $effectiveDate,其中包含日期2012-03-26

我尝试在这个日期上加三个月,但一直没有成功。

以下是我尝试过的方法:

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
并且
$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");

我做错了什么?两段代码都没有起作用。


6
“didn't work” 的意思是“没有成功/没起作用”。 - Jon
2
我得到的答案是 1340649000,看起来是正确的。 - Dogbert
你确定 $effectiveDate 存储的是你想要的内容吗?这个链接对我有效 - Josh
我期望将日期从2012年03月26日加3个月得到2012年06月26日。 - user979331
date('Y-m-d', 1340661600) 给出的结果是 2012-06-26,这是正确的。 - Tchoupi
@user1193385 请仔细阅读PHP的strtotime函数。该函数期望传入一个包含英文日期格式的字符串,并尝试将其解析为Unix时间戳。 - PenguinCoder
11个回答

236

将其更改为以下内容即可获得预期的格式:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));

如何处理一个包含月份个数的变量?例如 $months 包含了月份的数量?"+ '$months' months" 无法正常工作。 - HKK
7
$offset = 5; echo date('Y-m-d', strtotime("+$offset months", strtotime('2000-01-01')));这段代码的作用是在日期'2000-01-01'上添加5个月,并将结果以YYYY-MM-DD的格式输出。 - Cees Timmerman
用strftime()代替date(),不要使用本地化功能。这样更好。 - user706420
5
这种方式无法处理像这样的日期:date('2018-01-31', strtotime('+ 3 month')). 如果日期以31结尾,而接下来的3个月没有31号,它将无法返回正确的日期。 - Dan H
3
不好的解决方案 - 无法返回正确的值 - 正如Dan H所提到的。 - Josef Vancura

37

这个答案并不完全回答这个问题。但是我会添加这个因为这个问题仍然可以被搜索到,用于如何从日期中增加/减少时间段。

$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;

6
我猜您所说的“没有成功”是指它给您返回了一个时间戳而不是格式化后的日期,因为您之前的操作是正确的。
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version

4

您需要将日期转换为易读的值。您可以使用strftime()或date()。

尝试这样做:

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;

这应该可以运行。我更喜欢使用strftime,因为它可以用于本地化,您可以尝试一下。

3

Tchoupi的答案可以通过将strtotime()函数的参数连接起来来简化:

$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );

(这个依赖于魔法实现细节,但是如果你有正确的不信任,你可以随时去查看它们。)

3
以下内容应该有效,请尝试这个:
$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);

2
这应该可以正常工作。
$d = strtotime("+1 months",strtotime("2015-05-25"));
echo   date("Y-m-d",$d); // This will print **2015-06-25** 

1
添加第n天、月和年
$n = 2;
for ($i = 0; $i <= $n; $i++){
    $d = strtotime("$i days");
    $x = strtotime("$i month");
    $y = strtotime("$i year");
    echo "Dates : ".$dates = date('d M Y', "+$d days");
    echo "<br>";
    echo "Months : ".$months = date('M Y', "+$x months");
    echo '<br>';
    echo "Years : ".$years = date('Y', "+$y years");
    echo '<br>';
}

1

从PHP 5.3开始,DateTimeDateInterval可以是实现所需结果的可行选项。

$months = 6; 

$currentDate = new DateTime();

$newDate = $currentDate->add(new DateInterval('P'.$months.'M'));

echo $newDate->format('Y-m-d');

如果您想从日期中减去时间,而不是使用add,请使用sub
以下是如何使用DateInterval的更多示例:
$interval = new DateInterval('P1Y2M3DT4H5M6S');

// This creates an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.

$interval = new DateInterval('P2W');

// This creates an interval of 2 weeks (which is equivalent to 14 days).

$interval = new DateInterval('PT1H30M');

// This creates an interval of 1 hour and 30 minutes (but no days or years, etc.).

我总是忘记 DateInterval 中的 P,谢谢您的发布。 - NobleUplift
我总是忘记DateInterval中的P,谢谢你的帖子。 - NobleUplift

0
public function getCurrentDate(){
    return date("Y-m-d H:i:s");
}

public function getNextDateAfterMonth($date1,$monthNumber){
   return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1))); 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接