LoginSignup
5
5

More than 5 years have passed since last update.

min()/max()関数でDateTimeオブジェクトを比較

Last updated at Posted at 2012-03-16
datetime-minmax.php
<?php
$dts = array(
    new DateTime('-5 days'),
    new DateTime('+1 weeks'),
    new DateTime('-3 years'),  // min
    new DateTime('-4 months'),
    new DateTime('+7 months'), // max
);

var_dump(max($dts), min($dts));

実行結果:

$ date --rfc-3339="seconds"; php datetime-minmax.php
2011-12-14 14:52:31+09:00
object(DateTime)#5 (3) {
  ["date"]=>
  string(19) "2012-07-14 14:52:31"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(10) "Asia/Tokyo"
}
object(DateTime)#3 (3) {
  ["date"]=>
  string(19) "2008-12-14 14:52:31"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(10) "Asia/Tokyo"
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5