そのつど調べるのが面倒なので記録しておこうと思う。
今日の日付を文字列として出力する
<?= date("Y-m-d") ?>
日付オブジェクトをフォーマットする
$target_date = new \DateTime('now'); <?= $target_date->format( "Y/m/d" ) ?>
ある日付文字列の,前後の日付を文字列として出力する
$target_dateには "2019-08-01" のような文字列が入っているものとする
<?= // 次の日 date( "Y-m-d", strtotime("1 day", strtotime( $target_date ) ) ) ?> <?= // 前の日 date( "Y-m-d", strtotime("-1 day", strtotime( $target_date ) ) ) ?> <?= // 1週間後 date( "Y-m-d", strtotime("7 day", strtotime( $target_date ) ) ) ?> <?= // 1週間前 date( "Y-m-d", strtotime("-7 day", strtotime( $target_date ) ) ) ?>
ある日付オブジェクトの,前後の日付を文字列として出力する
$target_dateは日付オブジェクトとする
<?php $calc_date = date( // フォーマット "Y-m-d", // もとになるDateTimeオブジェクト mktime( 0, 0, 0, $target_date->format("m"), $target_date->format("d") - 7, // 7日前 $target_date->format("Y") ) ); ?>
参考:
mktimeで日付の加減算
https://www.php.net/manual/ja/function.date.php
ある日付の曜日を出力する
<?= [ "日", "月", "火", "水", "木", "金", "土" ][ date("w", strtotime( "2019-08-01" ) ) ] ?>