将三个月添加到 PHP 中的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9875076/
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 three months to a date in PHP
提问by user979331
I have a variable called $effectiveDate
containing the date 2012-03-26.
我有一个名为变量$effectiveDate
包含日期2012-03-26。
I am trying to add three months to this date and have been unsuccessful at it.
我试图将这个日期增加三个月,但没有成功。
Here is what I have tried:
这是我尝试过的:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
and
和
$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");
What am I doing wrong? Neither piece of code worked.
我究竟做错了什么?这段代码都没有工作。
回答by Tchoupi
Change it to this will give you the expected format:
将其更改为此将为您提供预期的格式:
$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
回答by Sadee
This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.
这个答案不完全是这个问题。但我会添加这个,因为这个问题仍然可以搜索到如何从日期添加/扣除期限。
$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;
回答by Nick
I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:
我假设“不起作用”是指它给了你一个时间戳而不是格式化的日期,因为你做对了:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version
回答by gleech
Tchoupi'sanswer can be made a tad less verbose by concatenating the argument for strtotime() as follows:
通过按如下方式连接 strtotime() 的参数,Tchoupi 的答案可以变得不那么冗长:
$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );
(This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)
(这依赖于神奇的实现细节,但如果你确实不信任的话,你可以随时去看看它们。)
回答by Dipak kukadiya
The following should work,Please Try this:
以下应该工作,请试试这个:
$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
回答by JohnnyQ
You need to convert the date into a readable value. You may use strftime() or date().
您需要将日期转换为可读值。您可以使用 strftime() 或 date()。
Try this:
尝试这个:
$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;
This should work. I like using strftime better as it can be used for localization you might want to try it.
这应该有效。我更喜欢使用 strftime ,因为它可以用于您可能想尝试的本地化。
回答by Ricky
Following should work
以下应该工作
$d = strtotime("+1 months",strtotime("2015-05-25"));
echo date("Y-m-d",$d); // This will print **2015-06-25**
回答by Rahul Gandhi
Add nth Days, months and years
添加第 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>';
}
回答by Brendon Dugan
The following should work, but you may need to change the format:
以下应该有效,但您可能需要更改格式:
echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));
回答by isa
You can use simpleDate class from PHP Simple Libraries:
您可以使用 PHP 简单库中的 simpleDate 类:
include('../code/simpleDate.php');
$date = new simpleDate();
echo $date->set($effectiveDate)->addMonth(3)->get();
Check the library tutorials here.