コード例 #1
0
ファイル: DateRangeTest.php プロジェクト: misantron/php-utils
 public function testGetRangeWithGreaterBegin()
 {
     $range = new DateRange('2016-03-13', '2016-03-11');
     $actual = $range->getRange();
     $this->assertInternalType('array', $actual);
     $this->assertEquals('2016-03-13', $actual[0]);
     $this->assertEquals('2016-03-12', $actual[1]);
     $this->assertEquals('2016-03-11', $actual[2]);
     $this->assertCount(3, $actual);
 }
コード例 #2
0
ファイル: UTime.php プロジェクト: misantron/php-utils
 /**
  * Return seconds difference between two dates.
  *
  * @param mixed $periodStartDate
  * @param mixed $periodEndDate
  *
  * @return int
  *
  * @throws \InvalidArgumentException
  * @throws \OutOfRangeException
  * @throws \RuntimeException
  */
 public static function secondsDiff($periodStartDate, $periodEndDate)
 {
     /**
      * @var \DateTime $fromDate
      * @var \DateTime $toDate
      */
     // @codeCoverageIgnoreStart
     $dateRange = new DateRange($periodStartDate, $periodEndDate);
     $fromDate = $dateRange->getRangeBegin();
     $toDate = $dateRange->getRangeEnd();
     // @codeCoverageIgnoreEnd
     $diff = $fromDate->diff($toDate);
     // @codeCoverageIgnoreStart
     if ($diff === false) {
         throw new \RuntimeException('Unexpected runtime error.');
     }
     // @codeCoverageIgnoreEnd
     if ($diff->y > 0 || $diff->m > 0) {
         throw new \OutOfRangeException('Date diff may not exceed one month.');
     }
     $seconds = 0;
     if ($diff->d >= 1) {
         $seconds += $diff->d * 24 * 60 * 60;
     }
     if ($diff->h >= 1) {
         $seconds += $diff->h * 60 * 60;
     }
     if ($diff->i >= 1) {
         $seconds += $diff->i * 60;
     }
     if ($diff->s >= 1) {
         $seconds += $diff->s;
     }
     return $diff->invert === 0 ? $seconds : -$seconds;
 }