LoginSignup
2
6

More than 5 years have passed since last update.

一番簡単にPHPで1ヶ月前の日付とかを取得する関数

Last updated at Posted at 2016-11-16
sample.php
//1ヶ月前
echo date('Y-m-d', strtotime("-1 month"));
//2016-10-16

//現在
echo date('Y-m-d', strtotime("now"));
echo date('Y-m-d', time());
//2016-11-16

//1ヶ月後
echo date('Y-m-d', strtotime("+1 month"));
//2016-12-16

こんな感じで +1 dayとか +1 yearとかいう書き方が通用する
yesterdayとかって書いてもいけちゃう。

ちなみにLaravelだったら、

sample.blade.php
{{ date('Y-m-d', strtotime("-1 month")) }}

こう書く。

日付扱う時にはこれ最強に便利。

2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6