【覚書】PHP、日付・時刻処理のメモ

スポンサー

※当サイトではアフィリエイトプログラムを利用して商品を紹介しています。

WEBの申込フォームのプログラミングで、
2ヶ月後には、自動的にリストには表示されないようにして欲しいとか、
有効期限は、2週間とか、有効な開始日は、今日から3日後からとかいった場合、
開始日、終了日の自動取得が必要です。

PHPでの日付のフォーマット化、日付の取得、ときどき使うのだけれど、覚えられないので、メモとして残しておきます。

[tips]日付の処理は、mktime/strtotime/dateを使う。[/tip]

□今日の日付
$d = date(“Y-m-d”,mktime());

□今日から○○日後
ex.今日から2日後
$d = date(“Y-m-d”,strtotime(“+2 day”,mktime()));

□今日から○○ヶ月後
ex.今日から2ヶ月後
$d = date(“Y-m-d”,strtotime(“+2 month”,mktime()));

□今日から○○ヶ月と○○日後
ex.今日から2ヶ月と2日後
$d = date(“Y-m-d”,strtotime(“+2 day”,strtotime(“+2 month”,mktime())));

□xx年xx月xx日から○○日後
ex.2012年1月1日から2日後
$st = “2012-01-01”;
$d = date(“Y-m-d”,strtotime(“+2 day”,strtotime($st)));

□xx年xx月xx日から○○ヶ月後
ex.2012年1月1日から2ヶ月後
$st = “2012-01-01”;
$d = date(“Y-m-d”,strtotime(“+2 month”,strtotime($st)));

□xx年xx月xx日から○○ヶ月と○○日後
ex.2012年1月1日から2ヶ月と2日後
$st = “2012-01-01”;
$d = date(“Y-m-d”,strtotime(“+2 day”,strtotime(“+2 month”,strtotime($st))));

[note]strtotimeで、日付をタイムスタンプ型に変更して、日数、月数をプラスするのがキモ。[/note]

PHP
スポンサー
コネクト

コメント

タイトルとURLをコピーしました