LoginSignup
43
38

More than 5 years have passed since last update.

特定の日付の1日前とかを取得する

Posted at

いつも忘れるので備忘録として。

普通に、現在時間から計算して、1日前/1日後などは簡単に出せます。

現在時間から特定の日時を取得

// 1時間前
echo date('Y-m-d H:i:s', strtotime('-1 hour', time()));

// 1週間前
echo date('Y-m-d H:i:s', strtotime('-1 week', time()));

指定時間から特定の日時を取得

単純にstrtotimeの第二引数に指定時間のタイムスタンプを渡してあげればOKでした。


// 3/10 0時を基準に考える
$targetTime = strtotime('2015-03-10 00:00:00');

// 3/10 0時の1時間前
echo date('Y-m-d H:i:s', strtotime('-1 hour', $targetTime)); // => 2015-03-09 23:00:00

// 3/10 0時の1週間前
echo date('Y-m-d H:i:s', strtotime('-1 week', $targetTime)); // => 2015-03-03 00:00:00 
43
38
2

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
43
38