如何在不跳过的日期上添加 1 个月,即 2 月


How to add 1 month on a date without skipping i.e. february

可能的重复项:
PHP 日期时间::修改加减月

我有一个开始日期(即 2011-01-30),并希望添加 1 个月。

问题在于定义一个月是什么。因此,如果我使用以下代码:

$d1 = DateTime::createFromFormat('Y-m-d H:i:s', '2011-01-30 15:57:57');
$d1->add(new DateInterval('P1M'));
echo $d1->format('Y-m-d H:i:s');

我得到以下结果: 2011-03-02 15:57:57

问题是,我需要它使用以下规则:

    如果我添加 1
  • 个月,它将只在月份部分添加 1 并离开日期部分(2011-01-15 将变为 2011-02-15)
  • 如果我们将结束的月份中不存在该日期,则采用该日期的最后一天(2011-01-30 将变为 2011-02-28)

php 中是否有一个通用函数可以做到这一点,还是我必须自己编写代码?也许我只是缺少参数或其他东西!?

似乎没有现成的功能,所以我自己写的。这应该可以解决我的问题。无论如何,感谢您的回答和评论。如果您发现一些错误,请在评论中提供。

此函数计算添加某个月份后我将结束的月份和年份。然后它会检查日期是否正确。如果不是,我们的情况是该天不在目标月份中,因此我们改用该月的最后一天。使用 PHP 5.3.10 进行测试。

<?php
$monthToAdd = 1;
$d1 = DateTime::createFromFormat('Y-m-d H:i:s', '2011-01-30 15:57:57');
$year = $d1->format('Y');
$month = $d1->format('n');
$day = $d1->format('d');
$year += floor($monthToAdd/12);
$monthToAdd = $monthToAdd%12;
$month += $monthToAdd;
if($month > 12) {
    $year ++;
    $month = $month % 12;
    if($month === 0)
        $month = 12;
}
if(!checkdate($month, $day, $year)) {
    $d2 = DateTime::createFromFormat('Y-n-j', $year.'-'.$month.'-1');
    $d2->modify('last day of');
}else {
    $d2 = DateTime::createFromFormat('Y-n-d', $year.'-'.$month.'-'.$day);
}
$d2->setTime($d1->format('H'), $d1->format('i'), $d1->format('s'));
echo $d2->format('Y-m-d H:i:s');

除了 DateInterval,您还有其他选择。

以下是使用 strtotime() 的示例:

http://www.brightcherry.co.uk/scribbles/php-adding-and-subtracting-dates/

// Subtracting days from a date
$date = "1998-08-14";
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;

// Subtracting months from a date
$date = "1998-08-14";
$newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;

以下是一些DateInterval链接:

  • http://php.net/manual/en/dateinterval.construct.php

  • 使用 DateInterval 和 DateTime::add 添加月份时会出现什么问题?

问:您究竟如何定义"月"?

Def#1):"月"

就是"月" - 无论 #/天如何

Def#2):"月"是 30 天(例如)

Def#3):"月"是第一个星期一之间的 #/天 随后的几个月

等等

等等

问:您的"定义"到底是什么?

好的 - 据我所知,您仍然没有明确定义"月"的含义。

但这里有一个可能有帮助的函数:

http://php.net/manual/en/function.getdate.php

/* Function to find the first and last day of the month from the given date.
*
* Author Binu v Pillai            binupillai2003@yahoo.com
* @Param            String             yyyy-mm-dd
*
*/
function findFirstAndLastDay($anyDate)
{
    //$anyDate            =    '2009-08-25';    // date format should be yyyy-mm-dd
    list($yr,$mn,$dt)    =    split('-',$anyDate);    // separate year, month and date
    $timeStamp            =    mktime(0,0,0,$mn,1,$yr);    //Create time stamp of the first day from the give date.
    $firstDay            =     date('D',$timeStamp);    //get first day of the given month
    list($y,$m,$t)        =     split('-',date('Y-m-t',$timeStamp)); //Find the last date of the month and separating it
    $lastDayTimeStamp    =    mktime(0,0,0,$m,$t,$y);//create time stamp of the last date of the give month
    $lastDay            =    date('D',$lastDayTimeStamp);// Find last day of the month
    $arrDay                =    array("$firstDay","$lastDay"); // return the result in an array format.
    return $arrDay;
}