繁体   English   中英

如何从今天的日期获取下个月的日期并将其插入到我的数据库中?

[英]How do I get next month date from today's date and insert it in my database?

我的数据库中有两列: 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 的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)));

接受的答案仅在您想要 31 天后才有效。 这意味着如果您使用的日期“2013-05-31”,您预计不会在 6 月,这不是我想要的。

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

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

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

您实际上并不需要 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

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

// 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

小心,因为有时strtotime("+1 months")可以跳过月份数字。

例子:

$today = date("Y-m-d"); // 2012-01-30
$next_month = date("Y-m-d", strtotime("$today +1 month"));

如果今天是一月,那么下个月应该是二月,有 28 或 29 天,但 PHP 会将三月返回为下个月,而不是二月。

date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))

您可以在 Gazler 的示例中使用strtotime() ,这对这种情况非常有用。

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

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

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

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

此函数正负返回任何正确的月数。 这里评论部分找到

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;
}

2014 年 2 月 1 日

$date = mktime( 0, 0, 0, 2, 1, 2014 );

echo strftime( '%d %B %Y', strtotime( '+1 month', $date ) );

我认为这类似于 kouton 的答案,但这个答案只是接受一个时间戳并在下个月的某个地方返回一个时间戳。 您可以从那里使用 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);
}

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

    $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)。 如果该服务于 2019 年 3 月 31 日开始并运行 11 个月,则将于 2020 年 2 月 29 日结束。如果仅运行 1 个月,则结束日期为 2019 年 4 月 30 日。

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

这样做的一种方法是 - 首先获取当月的最后日期,然后添加 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM