diff --git a/.gitignore b/.gitignore index 44fccc5..8b40964 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ reports/ build/ coverage/ .idea/ - +composer.lock diff --git a/composer.json b/composer.json index dbf6683..c2f6e12 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "hughgrigg/php-business-time", "description": "Business time / working days extension for Carbon dates", - "version": "1.1.1", + "version": "1.1.2", "type": "library", "require": { "php": "^7.0", diff --git a/src/Interval.php b/src/Interval.php index 6fa5ca3..046ea66 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -81,21 +81,36 @@ public function forHumans($short = false): string * Normalise instance creation to seconds. * * @param DateInterval $dateInterval + * @param bool $trimMicroseconds * * @return static */ - public static function instance(DateInterval $dateInterval): self - { - return self::seconds(self::intervalToSeconds($dateInterval)); + public static function instance( + DateInterval $dateInterval, + $trimMicroseconds = true + ): self { + return self::seconds( + self::intervalToSeconds($dateInterval, $trimMicroseconds) + ); } /** - * @param DateInterval $interval + * @param DateInterval $dateInterval + * @param bool $trimMicroseconds * * @return int */ - public static function intervalToSeconds(DateInterval $interval): int - { - return (new Carbon())->add($interval)->diffInRealSeconds(new Carbon()); + public static function intervalToSeconds( + DateInterval $dateInterval, + $trimMicroseconds = true + ): int { + $dateInterval->f = + $trimMicroseconds || + version_compare(PHP_VERSION, '7.1.0-dev', '<') ? 0 : + $dateInterval->f; + + return (new Carbon())->add($dateInterval)->diffInRealSeconds( + new Carbon() + ); } }