PHP 添加 1 个月至今
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12324640/
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
PHP add 1 month to date
提问by Nolly Parpan
I've a function that returns url of 1 month before.
我有一个返回 1 个月前 url 的函数。
I'd like to display current selected month, but I cannot use simple current month, cause when user clicks link to 1 month back selected month will change and will not be current.
我想显示当前选定的月份,但我不能使用简单的当前月份,因为当用户单击指向 1 个月后选定月份的链接时,会更改并且不会是当前月份。
So, function returns August 2012
因此,函数返回 2012 年 8 月
How do I make little php script that adds 1 month to that?
我如何制作增加 1 个月的小 php 脚本?
so far I've:
到目前为止我已经:
<?php echo strip_tags(tribe_get_previous_month_text()); ?>
回答by Marc B
simple method:
简单的方法:
$next_month = strtotime('august 2012 next month');
better method:
更好的方法:
$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));
relevant docs: strtotimedatedateinterval
相关文档:strtotime date dateinterval
回答by Nolly Parpan
there are 3 options/answers
有 3 个选项/答案
$givendate is the given date (ex. 2016-01-20)
option 1:
$date1 = date('Y-m-d', strtotime($givendate. ' + 1 month'));
option 2:
$date2 = date('Y-m-d', strtotime($givendate. ' + 30 days'));
option 3:
$number = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($givendate)), date('Y', strtotime($givendate)));
$date3 = date('Y-m-d', strtotime($date2. ' + '.$number.' days'));
回答by Bgi
You can with the DateTime class and the DateTime::add() method:
您可以使用 DateTime 类和 DateTime::add() 方法:
回答by Oliver Dechant
You can simple use the strtotimefunction on whatever input you have to arrive at April 2012 then apply the dateand strtotimewith an increment period of '+1 month'.
您可以strtotime在 2012 年 4 月必须到达的任何输入上简单地使用该函数,然后应用date和 并strtotime以“+1 个月”为增量周期。
$x = strtotime($t);
$n = date("M Y",strtotime("+1 month",$x));
echo $n;
Here are the relevant sections from the PHP Handbook:
以下是 PHP 手册中的相关部分:
http://www.php.net/manual/en/function.date.php
http://www.php.net/manual/en/function.date.php
https://secure.php.net/manual/en/function.strtotime.php
https://secure.php.net/manual/en/function.strtotime.php
This solution solves the additional issue of incrementing anyamount of time to a time value.
此解决方案解决了将任何时间量增加到时间值的附加问题。
回答by vlad awtsu
Hi In Addition to their answer. I think if you just want to get the next month based on the current date here's my solution.
嗨,除了他们的回答。我想如果您只想根据当前日期获得下个月,这是我的解决方案。
$today = date("Y-m-01");
$sNextMonth = (int)date("m",strtotime($today." +1 months") );
Notice That i constantly define the day to 01 so that we're safe on getting the next month. if that is date("Y-m-d"); and the current day is 31 it will fail.
请注意,我不断将日期定义为 01,以便我们可以安全地获得下个月。如果那是日期(“Ymd”);并且当前日期是 31 它将失败。
Hope this helps.
希望这可以帮助。
回答by Kamlesh
Date difference
日期差异
$date1 = '2017-01-20';
$date2 = '2019-01-20';
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);
$month1 = date('m', $ts1);
$month2 = date('m', $ts2);
echo $joining_months = (($year2 - $year1) * 12) + ($month2 - $month1);

