『葉狂人』的怕失憶筆記 Back-End

PHP 增加一個月日期的方法

[PHP bug]
如果想算出下個月的此時, 一般會使用
$next_month = date('Y-m-d', strtotime('+1 month'));

問題:
但假設今天為5月31日, 結果則會變成7/1,
由於6月沒有31號, 所以自動進到下個月去.

解決方法:
先取得當月的第一天, 在進行計算即可
$first_day = date('Y-m-d', strtotime('first day of this month'));
$next_month = date('Y-m-d', strtotime($first_day . '+1 month'));

有算月份需求的趕緊改正吧!

『葉狂人』的怕失憶筆記, author
「我從不為輸贏煩惱,我為樂趣而跑。」
F1車神舒馬克