Are PHP DateInterval comparable like DateTime?

前端 未结 7 2093
孤城傲影
孤城傲影 2020-12-03 13:36

I discovered that a DateTime object in PHP can be compared to another as the \">\" and \"<\" operators are overloaded.

Is it the same with DateInterval?

A

7条回答
  •  旧时难觅i
    2020-12-03 14:07

    If you're working with time intervals that are not longer than a month, it's easy to convert 2 intervals to seconds and compare. $dateInterval->format("%s") only returns the seconds component so I ended up doing this:

    function intervalToSeconds($dateInterval) {
            $s = (
                ($dateInterval->format("%d")*24*60*60) + 
                ($dateInterval->format("%h")*60*60) + 
                ($dateInterval->format("%i")*60) + 
                $dateInterval->format("%s")
            );
            return $s;
        }
    

提交回复
热议问题