- 初始化
|
|
设置时区
12$d = new DateTime(null, new DateTimezone('Asia/Shanghai'));$d->setTimezone('Asia/Shanghai');设置/获取时间戳
12echo $d->getTimestamp(); //1417664087echo $d->setTimestamp(1417664087)->format('Y-m-d H:i:s'); //2014-12-04 11:34:47修改时间
123//明天当前时间 (使用关键字day、week、month year 进行修改)使用modifyecho $d->modify('+1 day')->setTime(0, 0, 0)->format('Y-m-d H:i:s');
或者使用 sub、add12echo $d->sub(new DateInterval('P1D'))->format('Y-m-d H:i:s');echo $d->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
比较日期大小
12345$t = new DateTimezone('Asia/Shanghai');$d1 = new DateTime('2014-12-01', $t);$d2 = new DateTime('2014-12-02', $t);echo $d1 > $d2 ? 'd1 bigger than d2' : 'd1 less than d2';获取日期间的差异
12345678910111213141516171819202122$diff = $d1->diff($d2);print_r($diff);DateInterval Object([y] => 0[m] => 0[d] => 1[h] => 0[i] => 0[s] => 0[weekday] => 0[weekday_behavior] => 0[first_last_day_of] => 0[invert] => 0[days] => 1[special_type] => 0[special_amount] => 0[have_weekday_relative] => 0[have_special_relative] => 0)