DateTimeクラス

マニュアル: http://php.net/manual/ja/class.datetime.php
PHP5.2からDateTimeクラスが使えます。
まずは書いてみます。
$now = new DateTime();
print $now->format('Y年m月d日 H:i:s');    
結果
2024年03月19日 13:58:40次に引数を入れてみます。
$now = new DateTime('2010/5/15 10:58:31');
print $now->format('Y年m月d日 H時i分');
結果
2010年05月15日 10時58分他の指定も見てみます。
$now1 = new DateTime('last monday');
$now2 = new DateTime('last Saturday');
$now3 = new DateTime('yesterday');
$now4 = new DateTime('today');
$now5 = new DateTime('tomorrow');
$now6 = new DateTime('last year');
print $now1->format('Y年m月d日 H時i分')."
"; print $now2->format('Y年m月d日 H時i分')."
"; print $now3->format('Y年m月d日 H時i分')." 昨日
"; print $now4->format('Y年m月d日 H時i分')." 今日
"; print $now5->format('Y年m月d日 H時i分')." 明日
"; print $now6->format('Y年m月d日 H時i分')." 去年
";

またそれぞれの日時と時間をそれぞれセットできる。
$now = new DateTime();
$now->setDate(2010, 6, 25);
$now->setTime(14, 35, 59);
print $now->format('Y年m月d日 H:i:s');

例えば、90分は1時間30分丸めてくれるか確認
大丈夫みたいですね。
タイムスタンプも入れられるらしい。
書いてみます。
$now = new DateTime();
$now->setTimestamp(time());
print $now->format('Y年m月d日 H:i:s');
// 5.2で動作しなかった・・・後で検証。

次に時間を差分を取得してみます。
$dt1 = new DateTime('2010/5/15 10:58:31');
$dt2 = new DateTime('2010/12/04');
$interval = $dt1->diff($dt2, TRUE);
print $interval->format('%Y年%M月%d日 %H時間%I分%S秒');



//この記事は編集中です。