2014年6月25日水曜日

PHPの日付の加算、減算の備忘録





// 現在日付から○日の取得

echo "1日前" . date("Y/m/d", strtotime("-1 day" ));
echo "1ヶ月前" . date("Y/m/d", strtotime("-1 month"));
echo "1年前" . date("Y/m/d", strtotime("-1 year" ));
echo "1週間前" . date("Y/m/d", strtotime("-1 week" ));


// 指定日付から○日の取得

echo "1日前" . date("Y/m/d", strtotime("2007/12/20 -1 day" ));
echo "1ヶ月前" . date("Y/m/d", strtotime("2007/12/20 -1 month"));
echo "1年前" . date("Y/m/d", strtotime("2007/12/20 -1 year" ));
echo "1週間前" . date("Y/m/d", strtotime("2007/12/20 -1 week" ));


こういうのって、すぐに書き方を忘れてしまうので


書き留めておくと便利かも。





関連リンク:PHPで、先月、翌月などのを扱うときの注意





0 件のコメント: