Tools\Utility\Time::difference PHP Method

difference() public static method

TODO: deprecate in favor of DateTime::diff() etc which will be more precise should only be used for < month (due to the different month lenghts it gets fuzzy)
public static difference ( mixed $startTime, mixed | null $endTime = null, array $options = [] ) : integer
$startTime mixed (db format or timestamp)
$endTime mixed | null (db format or timestamp)
$options array
return integer The distance in seconds
    public static function difference($startTime, $endTime = null, array $options = [])
    {
        if (!is_int($startTime)) {
            $startTime = strtotime($startTime);
        }
        if (!is_int($endTime)) {
            $endTime = strtotime($endTime);
        }
        //@FIXME: make it work for > month
        return abs($endTime - $startTime);
    }

Usage Example

コード例 #1
0
ファイル: TimeTest.php プロジェクト: alescx/cakephp-tools
 /**
  * TimeTest::testDifference()
  *
  * @return void
  */
 public function testDifference()
 {
     //$this->out($this->_header(__FUNCTION__), true);
     $values = [['2010-02-23 11:11:11', '2010-02-23 11:12:01', 50], ['2010-02-23 11:11:11', '2010-02-24 11:12:01', DAY + 50]];
     foreach ($values as $v) {
         $ret = $this->Time->difference($v[0], $v[1]);
         $this->assertEquals($v[2], $ret);
     }
 }