【问题标题】:How do I get next month date from today's date and insert it in my database?如何从今天的日期获取下个月的日期并将其插入我的数据库中?
【发布时间】:2010-11-30 21:43:42
【问题描述】:

我的数据库中有两列:start_dateend_date,它们都是 DATE 类型。我的代码更新日期如下:

$today_date = date("Y-m-d");
$end_date = date("Y-m-d"); // date +1 month ??

$sql1 = "UPDATE `users` SET `start_date` = '".$today_date."', `end_date` = '".$end_date."'  WHERE `users`.`id` ='".$id."' LIMIT 1 ;";

使$end_date 等于$start_date + 一个月的最佳方法是什么?例如,2000-10-01 将变为 2000-11-01。

【问题讨论】:

标签: php mysql date


【解决方案1】:

可以使用PHP的strtotime()函数:

// One month from today
$date = date('Y-m-d', strtotime('+1 month'));

// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));

请注意,+1 month 并不总是直观地计算出来的。它似乎总是添加当前月份中存在的天数。

Current Date  | +1 month
-----------------------------------------------------
2015-01-01    | 2015-02-01   (+31 days)
2015-01-15    | 2015-02-15   (+31 days)
2015-01-30    | 2015-03-02   (+31 days, skips Feb)
2015-01-31    | 2015-03-03   (+31 days, skips Feb)
2015-02-15    | 2015-03-15   (+28 days)
2015-03-31    | 2015-05-01   (+31 days, skips April)
2015-12-31    | 2016-01-31   (+31 days)

您可以使用的其他一些日期/时间间隔:

$date = date('Y-m-d'); // Initial date string to use in calculation

$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+2 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 month', strtotime($date)));
$date = date('Y-m-d', strtotime('+30 days', strtotime($date)));

【讨论】:

  • 谢谢 - 我不知道 PHP 足够聪明,可以将 +1 month 变成时间!我认为 PHP 从当前时间开始计算 +1 个月(显然​​)?
  • 没错。玩一玩,它还接受诸如“下周四”、“上周二”和“+1 周 3 天 2 小时 4 秒”之类的内容
  • @JamWaffles 和 @SzamDev,当当前日期为 1 月 31 日时,您应该检查您对结果是否满意。带有月份的日期算术有时会产生意想不到的结果。
  • 如果日期是 2014 年 1 月 30 日或 2014 年 1 月 31 日,这将不起作用
  • 我编辑了这个问题,以便更清楚地说明日期在哪里可能有点混乱。谢谢@DonKirkby。
【解决方案2】:

仅当您希望在 31 天后才有效。这意味着如果您使用的日期是“2013-05-31”,而您预计不会出现在 6 月,这不是我想要的。

如果你想有下个月,我建议你使用当前年份和月份,但继续使用第一个。

$date = date("Y-m-01");
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;

这样,您将能够获得下个月的月份和年份,而不会跳过一个月。

【讨论】:

  • 是的,谢谢@Patrick,我对接受的答案进行了编辑,以注意它有时进行计算的令人困惑的方式。
  • 谢谢。这很聪明。
  • $nextm = date('m', strtotime('+1 month', strtotime(date('Y-m-01'))));
【解决方案3】:

您实际上并不需要 PHP 函数来实现这一点。您已经可以直接在 SQL 中进行简单的日期操作,例如:

$sql1 = "
    UPDATE `users` SET 
    `start_date` = CURDATE(), 
    `end_date` = DATE_ADD(CURDATE(), INTERVAL 1 MONTH)  
    WHERE `users`.`id` = '" . $id . "';
";

参考http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime

【讨论】:

  • 我打算发布与此类似的答案,但我不太记得该怎么做 - 感谢您发布解决方案。 +1
【解决方案4】:

如果您想要下个月的具体日期,您可以这样做:

// Calculate the timestamp
$expire = strtotime('first day of +1 month');

// Format the timestamp as a string
echo date('m/d/Y', $expire);

请注意,这实际上在+1 month 可能令人困惑的地方更可靠。比如……

Current Day   | first day of +1 month     | +1 month
---------------------------------------------------------------------------
2015-01-01    | 2015-02-01                | 2015-02-01
2015-01-30    | 2015-02-01                | 2015-03-02  (skips Feb)
2015-01-31    | 2015-02-01                | 2015-03-03  (skips Feb)
2015-03-31    | 2015-04-01                | 2015-05-01  (skips April)
2015-12-31    | 2016-01-01                | 2016-01-31

【讨论】:

  • 这是一个非常重要的答案。我发现奇怪的是,我在 5 月 31 日的代码是从 +1 月命令中得出的 7 月。这样就可以解释了!
【解决方案5】:

在这里添加我的解决方案,因为这是谷歌搜索中的主题。这是为了获得下一个月的日期,修复任何跳过,将下一个日期保持在下个月内。

PHP 将当前月份的总天数添加到当前日期,例如 +1 month

因此将+1 month 应用于30-01-2016 将返回02-03-2016。 (加31天)

对于我的情况,我需要得到28-02-2016,以便在下个月内收到。在这种情况下,您可以使用以下解决方案。

此代码将识别给定日期的天数是否大于下个月的总天数。如果是这样,它将巧妙地减去天数并返回月份范围内的日期。

请注意返回值是时间戳格式。

function getExactDateAfterMonths($timestamp, $months){
    $day_current_date            = date('d', $timestamp);
    $first_date_of_current_month = date('01-m-Y', $timestamp);
    // 't' gives last day of month, which is equal to no of days
    $days_in_next_month          = date('t',  strtotime("+".$months." months", strtotime($first_date_of_current_month)));

    $days_to_substract = 0;
    if($day_current_date > $days_in_next_month)
         $days_to_substract = $day_current_date - $days_in_next_month;

    $php_date_after_months   = strtotime("+".$months." months", $timestamp);
    $exact_date_after_months = strtotime("-".$days_to_substract." days", $php_date_after_months);

    return $exact_date_after_months;
}

getExactDateAfterMonths(strtotime('30-01-2016'), 1);
// $php_date_after_months   => 02-03-2016
// $exact_date_after_months => 28-02-2016

【讨论】:

    【解决方案6】:

    您可以像 Gazler 的示例中那样使用 strtotime(),这非常适合这种情况。

    如果您需要更复杂的控制,请使用mktime()

    $end_date = mktime(date("H"), date("i"), date("s"), date("n") + 1, date("j"), date("Y"));
    

    【讨论】:

    • 这会不会很慢?所有这些date() 函数?
    • 这些是函数的默认值,用于说明。您可以使用单个date(),将其拆分并扔进去。很难说哪个会最快返回。 strtotime(),另一种选择,远没有人们想象的那么快。
    【解决方案7】:
    date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))
    

    【讨论】:

    • 虽然这段代码可能会回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用处。
    【解决方案8】:

    此函数返回任何 正确 正数或负数的月数。发现在comment section here

    function addMonthsToTime($numMonths = 1, $timeStamp = null){
        $timeStamp === null and $timeStamp = time();//Default to the present
        $newMonthNumDays =  date('d',strtotime('last day of '.$numMonths.' months', $timeStamp));//Number of days in the new month
        $currentDayOfMonth = date('d',$timeStamp);
    
        if($currentDayOfMonth > $newMonthNumDays){
          $newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days '.$numMonths.' months', $timeStamp);
        } else {
        $newTimeStamp = strtotime($numMonths.' months', $timeStamp);
        }
    
        return $newTimeStamp;
    }
    

    【讨论】:

      【解决方案9】:

      2014 年 2 月 1 日

      $date = mktime( 0, 0, 0, 2, 1, 2014 );
      
      echo strftime( '%d %B %Y', strtotime( '+1 month', $date ) );
      

      【讨论】:

        【解决方案10】:

        我认为这类似于 kouton 的答案,但这个只是接受一个 timeStamp 并在下个月返回一个时间戳 SOMEWHERE。您可以从那里使用 date("m", $nextMonthDateN) 。

        function nextMonthTimeStamp($curDateN){ 
           $nextMonthDateN = $curDateN;
           while( date("m", $nextMonthDateN) == date("m", $curDateN) ){
              $nextMonthDateN += 60*60*24*27;
           }
           return $nextMonthDateN; //or return date("m", $nextMonthDateN);
        }
        

        【讨论】:

          【解决方案11】:

          我知道 - 有点晚了。但我正在解决同样的问题。如果客户购买了一个月的服务,他/她预计会在一个月后结束。以下是我的解决方法:

              $now = time(); 
              $day = date('j',$now);
              $year = date('o',$now);
              $month = date('n',$now);
              $hour = date('G');
              $minute = date('i');
              $month += $count;
              if ($month > 12)        {
                      $month -= 12;
                      $year++; 
                      }
              $work = strtotime($year . "-" . $month . "-01");
              $avail = date('t',$work);
              if ($day > $avail)
                      $day = $avail;
              $stamp = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);
          

          这将计算从现在起 n*count 个月后的确切日期(其中 count

          【讨论】:

            【解决方案12】:

            $nextm = date('m', strtotime('+1 月', strtotime(date('Y-m-01'))));

            【讨论】:

              【解决方案13】:

              这样做的一种方法是 - 首先获取当前月份的最后一个日期,然后添加 1 天,以获得下个月的第一个日期。这将防止我们在使用“+1 月”时意外跳过月份。

              $today_date = date("Y-m-d"); 
              $last_date_of_month=date('Y-m-t',strtotime($today_date);//get last date of current month
              $last_day_of_month_proper =strtotime($last_day_of_month);
              $new_date=date('Y-m-d', strtotime('+1 day',$last_day_of_month_proper)); 
              echo $new_date;
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2023-04-03
                • 2016-03-28
                • 2017-06-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多