あぼかどブログ > Web > PHP > (phpメモ)本日を起点にした年月日を加算、減算で取得する

(phpメモ)本日を起点にした年月日を加算、減算で取得する

phpで本日or指定した日付を起点にした年月日を加算、減算で取得する(メモ)。
<例>
本日(仮に2013/02/28とする)を起点に、2週間後の年月日を取得。
$ymd = date('Y/m/d', strtotime('2 week'));
 ↓↓↓↓
$ymdは「2013/03/14」となる。

php:日付の加算・減算の例


<本日を起点にする場合>
【1日前】 date('Y/m/d', strtotime('-1 day'));
【1週間前】 date('Y/m/d', strtotime('-1 week'));
【1ヶ月前】 date('Y/m/d', strtotime('-1 month'));
【1年前】 date('Y/m/d', strtotime('-1 year' ));

【1日後】 date('Y/m/d', strtotime('1 day'));
【1週間後】 date('Y/m/d', strtotime('1 week'));
【1ヶ月後】 date('Y/m/d', strtotime('1 month'));
【1年後】 date('Y/m/d', strtotime('1 year'));

<指定日付を起点にする場合>
【1日前】 date('Y/m/d', strtotime('2013/03/01 -1 day'));
【1週間前】 date('Y/m/d', strtotime('2013/03/01 -1 week'));
【1ヶ月前】 date('Y/m/d', strtotime('2013/03/01 -1 month'));
【1年前】 date('Y/m/d', strtotime('2013/03/01 -1 year'));

【1日後】 date('Y/m/d', strtotime('2013/03/01 1 day'));
【1週間後】 date('Y/m/d', strtotime('2013/03/01 1 week'));
【1ヶ月後】 date('Y/m/d', strtotime('2013/03/01 1 month'));
【1年後】 date('Y/m/d', strtotime('2013/03/01 1 year' ));

php:本日から2週間後の年月日(曜日)を取得

$ymd = date('Y/m/d', strtotime('2 week'));
list($year, $month, $day) = explode('/', $ymd);
$weekAry = array('日', '月', '火', '水', '木', '金', '土');
$weekStr = $weekAry[date('w',mktime(0, 0, 0, $month, $day, $year))];
$date = $year . '年' . $month . '月' . $day .'日(' . $weekStr . ')';
 ↓↓↓↓
$dateは「2013年03月15日(金)」となる。

【前後の記事】
«
 
»
 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です