簡體   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