坑爹的php方法2 strtotime

http://d.hatena.ne.jp/bushimichi/20091101/1257007448/

 

 

寫道
echo '前月 => '. date('m', strtotime('2009-10-31 -1 month')) .'<br>';
echo '今月 => '. date('m', strtotime('2009-10-31') ) .'<br>';
echo '翌月 => '. date('m', strtotime('2009-10-31 +1 month')) .'<br>';


------ 結果 ------
先月 => 10
今月 => 10
翌月 => 12

 

解決方法是每次都要用當月的1號來做+-月的計算

 

寫道

echo '先月 => '. date('m', strtotime(date('Y-m-1').' -1 month')) .'<br>';
echo '今月 => '. date('m') .'<br>';
echo '翌月 => '. date('m', strtotime(date('Y-m-1').' +1 month')) .'<br>';

------ 結果 ------

前月 => 10
今月 => 11
翌月 => 12

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章