strtotime()

投稿日:2022-11-07 投稿者:PS カテゴリ:その他 タグ: , , ,

echo date('Y-m-d H:i:s', strtotime('now'));             // 2022-11-07 11:54:35 その時点の日時
echo date('Y-m-d H:i:s', strtotime('today'));           // 2022-11-07 00:00:00 その時点の日付(時刻は 0 時)
echo date('Y-m-d H:i:s', strtotime('+1 day'));          // 2022-11-08 11:54:35 翌日
echo date('Y-m-d H:i:s', strtotime('+1 month'));        // 2022-12-07 11:54:35 翌月
echo date('Y-m-d H:i:s', strtotime('last week'));       // 2022-10-31 11:54:35 先週の月曜日
echo date('Y-m-d H:i:s', strtotime('+1 year -1 day'));  // 2023-11-06 11:54:35 翌年の前日

// === [年] を2桁で指定する場合の注意 ===
// ハイフン(-)で区切る場合は問題ない
echo date('Y-m-d H:i:s', strtotime('23-12-01'));  // 2023-12-01 00:00:00
// スラッシュ(/)で区切る場合は、[月][日][年] の順にする必要がある。
echo date('Y-m-d H:i:s', strtotime('23/12/01'));  // 1970-01-01 09:00:00 ※ 日付として判断していない
echo date('Y-m-d H:i:s', strtotime('12/01/23'));  // 2023-12-01 00:00:00 OK
// ドット( . )で区切ると、
echo date('Y-m-d H:i:s', strtotime('23.12.01'));  // 2022-11-07 23:12:01 「時分秒」と判断した
echo date('Y-m-d H:i:s', strtotime('28.12.01'));  // 2001-12-28 00:00:00 「月日年」と判断した

// 注意すべき例
echo date('Y-m-d H:i:s', strtotime('2022-01-31 +1 month'));  // 2022-03-03 00:00:00
// 「2022-01-31 の翌月」は、2月31日と計算し、2月28日の3日後の 3月3日 になる