Rap*_*eta 149
$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));
// Finally you will have the date you're looking for.
Run Code Online (Sandbox Code Playgroud)
Jas*_*son 35
我需要类似的功能,除了每月周期(加上几个月,减去1天).在搜索了一段时间后,我能够制作这个即插即用的解决方案:
function add_months($months, DateTime $dateObject)
{
$next = new DateTime($dateObject->format('Y-m-d'));
$next->modify('last day of +'.$months.' month');
if($dateObject->format('d') > $next->format('d')) {
return $dateObject->diff($next);
} else {
return new DateInterval('P'.$months.'M');
}
}
function endCycle($d1, $months)
{
$date = new DateTime($d1);
// call second function to add the months
$newDate = $date->add(add_months($months, $date));
// goes back 1 day from date, remove if you want same day of month
$newDate->sub(new DateInterval('P1D'));
//formats final date to Y-m-d form
$dateReturned = $newDate->format('Y-m-d');
return $dateReturned;
}
Run Code Online (Sandbox Code Playgroud)
例:
$startDate = '2014-06-03'; // select date in Y-m-d format
$nMonths = 1; // choose how many months you want to move ahead
$final = endCycle($startDate, $nMonths); // output: 2014-07-02
Run Code Online (Sandbox Code Playgroud)
Mat*_*hen 31
$start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
$month_later = clone $start;
$month_later->add(new DateInterval("P1M"));
Run Code Online (Sandbox Code Playgroud)
我使用了clone,因为add修改了原始对象,这可能是不可取的.
Gal*_*len 13
strtotime( "+1 month", strtotime( $time ) );
Run Code Online (Sandbox Code Playgroud)
这将返回可与date函数一起使用的时间戳
请首先将日期格式设置为 12-12-2012
使用此功能后即可正常使用;
$date = date('d-m-Y',strtotime("12-12-2012 +2 Months");
Run Code Online (Sandbox Code Playgroud)
这里 12-12-2012 是您的日期,+2 Months 是月份的增量;
您还增加了年份、日期
strtotime("12-12-2012 +1 Year");
Run Code Online (Sandbox Code Playgroud)
答案是 12-12-2013
(date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
Run Code Online (Sandbox Code Playgroud)
这将补偿2月和其他31天的月份。当然,您可以做更多的检查来获得“下个月的这一天” 相对日期格式的更准确信息(这很难过,请参见下文),并且您也可以使用DateTime。
双方DateInterval('P1M')
并strtotime("+1 month")
基本上是盲目地在下个月加入31天不管天数。
我用这种方式: -
$occDate='2014-01-28';
$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=02
/*****************more example****************/
$occDate='2014-12-28';
$forOdNextMonth= date('m', strtotime("+1 month", strtotime($occDate)));
//Output:- $forOdNextMonth=01
//***********************wrong way**********************************//
$forOdNextMonth= date('m', strtotime("+1 month", $occDate));
//Output:- $forOdNextMonth=02; //instead of $forOdNextMonth=01;
//******************************************************************//
Run Code Online (Sandbox Code Playgroud)
您可以这样使用DateTime::modify
:
$date = new DateTime('2010-12-11');
$date->modify('+1 month');
Run Code Online (Sandbox Code Playgroud)
参见文档:
http://php.net/manual/fr/datetime.modify.php
http://php.net/manual/fr/class.datetime.php
小智 5
只需使用简单的方法更新答案即可找到几个月后的日期。由于标记的最佳答案并未给出正确的解决方案。
<?php
$date = date('2020-05-31');
$current = date("m",strtotime($date));
$next = date("m",strtotime($date."+1 month"));
if($current==$next-1){
$needed = date('Y-m-d',strtotime($date." +1 month"));
}else{
$needed = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
}
echo "Date after 1 month from 2020-05-31 would be : $needed";
?>
Run Code Online (Sandbox Code Playgroud)