PHPで月末を取得する方法(strtotime)

投稿者: | 2020年1月28日

PHPで月末を取得する方法(strtotime)です

// 対象年月
$yearMonth = '2020-01';

// 対象年月の月初取得
$firstDate = date('Y-m-d', strtotime('first day of ' . $yearMonth));

// 対象年月の月末取得
$lastDate = date('Y-m-d', strtotime('last day of ' . $yearMonth));

// その他の便利なstrtotimeの第一引数(日付/時刻 文字列)
// 'today '         今日
// 'yesterday '     昨日
// 'tomorrow '      明日
// '-1 day '        1日前
// '+3 day '        3日後
// '-1 week '       1週間前
// '+3 week '       3週間後
// '-1 month '      1ヶ月前
// '+3 month '      3ヶ月後
// '-1 year '       1年前
// '+3 year '       3年後
// 'next sunday '   次の日曜日
// 'next sun '      次の日曜日
// '+2 sunday '     次の次の日曜日
// '+2 sun '        次の次の日曜日
// 'last sunday '   前の日曜日
// 'last sun '      前の日曜日