2010-11-30 58 views
25

我的db中有兩列: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- -01將變成2000- -01。

+0

回答`的strtotime('+ 1個月)',之前看看http://www.gnu.org/software/tar/manual/html_node/Relative-items-in-date-strings .html#SEC120 – Gordon 2010-11-30 22:08:59

+0

如果`end_date`總是`start_date + 1 month`,它不應該在表中。如果沒有,這個評論不適用。 – Danosaure 2010-12-01 02:43:54

回答

66

您可以使用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))); 
+0

感謝您 - 我不知道PHP是否足夠聰明,可以將'+1個月'變成一次!我認爲PHP可以從當前時間+1個月(明顯)中計算出來嗎? – Bojangles 2010-11-30 21:47:32

+1

這是正確的。有一個遊戲,它也接受像「下週四」,「上週二」和「+1周3天2小時4秒」的東西 – Gazler 2010-11-30 21:48:33

+7

@JamWaffles和@SzamDev,你應該檢查你是否滿意目前的日期是1月31日。使用月份計算日期有時會產生意想不到的結果 – 2010-11-30 22:06:31

1

您可以使用strtotime()作爲Gazler的例子,這是偉大的這種情況。

如果您需要更復雜的控制使用mktime()

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

要小心,因爲有時候strtotime("+1 months")可以跳過月份的數字。

例子:

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

如果今天是一月,下個月應該是二月其中有28或29天,但PHP將返回三月下個月,而不是月。

15

接受的答案只有在31天后才需要。這意味着如果你使用的日期「2013-05-31」,你預計不會在六月這不是我想要的。

如果你想要下個月,我建議你使用當前的年份和月份,但繼續使用1st。

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

這樣,您將能夠獲得下個月的月份和年份而不會跳過一個月。

0

該函數返回正確或負數的任意正確的個月數。發現在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; 
} 
0

01 - 2月2014

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

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

如果你想在下個月某個特定日期,你可以這樣做:

// 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 
0
date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate))) 
0

我認爲這是類似於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); 
} 
相關問題