kcs / php-duration
Converts between colon formatted time, human-readable time and seconds
v2.0.0
2024-09-09 10:16 UTC
Requires
- php: ^8.2
Requires (Dev)
- doctrine/dbal: ^2.5 || ^3.0 || ^4.0
- kcs/serializer: ^4.0 || ^5.0@dev
- phpunit/phpunit: ^10.0 || ^11.0
- roave/security-advisories: dev-latest
- solido/php-coding-standards: dev-master
README
Convert durations between colon formatted time, human-readable time and seconds
The library can accept either in colon separated format, like 2:43 for 2 minutes and 43 seconds OR written as human-readable or abbreviated time, such as 6m21s for 6 minutes and 21 seconds.
Both can be converted into seconds and minutes with precision for easy storage into a database.
Seconds, colon separated, abbreviated, all three can be parsed and interchanged.
- supports hours, minutes, and seconds (with microseconds)
- humanized input supports any form of the words "hour", "minute", "seconds"
- Example, you could input 1h4m2s or 4 Hr. 32 Min.
This library is a revamped/revised/updated version of the khill/php-duration library.
Install
composer require kcs/php-duration
Usage
use Kcs\Duration\Duration; $duration = new Duration('7:31'); echo $duration->humanize(); // 7m 31s echo $duration->formatted(); // 7:31 echo $duration->toSeconds(); // 451 echo $duration->toMinutes(); // 7.5166 echo $duration->toMinutes(0); // 8 echo $duration->toMinutes(2); // 7.52
$duration = new Duration('1h 2m 5s'); echo $duration->humanize(); // 1h 2m 5s echo $duration->formatted(); // 1:02:05 echo $duration->toSeconds(); // 3725 echo $duration->toMinutes(); // 62.0833 echo $duration->toMinutes(0); // 62
// Configured for 6 hours per day $duration = new Duration('1.5d 1.5h 2m 5s', 6); echo $duration->humanize(); // 1d 4h 32m 5s echo $duration->formatted(); // 10:32:05 echo $duration->toSeconds(); // 37925 echo $duration->toMinutes(); // 632.083333333 echo $duration->toMinutes(0); // 632
$duration = new Duration('4293'); echo $duration->humanize(); // 1h 11m 33s echo $duration->formatted(); // 1:11:33 echo $duration->toSeconds(); // 4293 echo $duration->toMinutes(); // 71.55 echo $duration->toMinutes(0); // 72
Utilities
This package contains some utility classes to help integration with other libraries:
Kcs\Duration\Doctrine\DurationType
: doctrine type to help using the duration class with doctrine ORM and DBALKcs\Duration\Serializer\DurationHandler
: (de-)serialization handler to use withkcs/serializer
library