chmielewskitomasz / ranges
Simple tool to manage time ranges (sum & sub). No dependencies required
Installs: 353
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.8
- phpspec/phpspec: ^4.3
- phpstan/phpstan: ^0.9.2
- phpunit/phpunit: ^6.0.8 || ^5.7.15
- squizlabs/php_codesniffer: ^2.8.1
This package is not auto-updated.
Last update: 2024-11-18 07:13:29 UTC
README
What is this tool supposed to help me with?
Using this tool you can
- add multiple time ranges
- substract multiple time ranges
How to use this tool?
Simple...
To add ranges just...
require_once __DIR__ . '/vendor/autoload.php'; use Hop\Ranges\Range; use Hop\Ranges\StdRangesCalculator; $range1 = new Range( new \DateTime('2018-01-01 00:00:00'), new \DateTime('2018-01-02 00:05:00') ); $range2 = new Range( new \DateTime('2018-01-02 00:06:00'), new \DateTime('2018-01-02 00:07:00') ); $range3 = new Range( new \DateTime('2018-01-02 00:06:30'), new \DateTime('2018-01-02 00:10:00') ); $calculator = new StdRangesCalculator(); $ranges = $calculator->sum( $range1, $range2, $range3 ); print_r($ranges); // prints out Array ( [0] => Hop\Ranges\Range Object ( [dateFrom:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-01 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC ) [dateTo:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-02 00:05:00.000000 [timezone_type] => 3 [timezone] => UTC ) ) ----> // this is merged! [1] => Hop\Ranges\Range Object ( [dateFrom:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-02 00:06:00.000000 [timezone_type] => 3 [timezone] => UTC ) [dateTo:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-02 00:10:00.000000 [timezone_type] => 3 [timezone] => UTC ) ) )
To substract ranges do
require_once __DIR__ . '/vendor/autoload.php'; use Hop\Ranges\Range; use Hop\Ranges\StdRangesCalculator; $range1 = new Range( new \DateTime('2018-01-01 00:00:00'), new \DateTime('2018-01-05 00:05:00') ); $range2 = new Range( new \DateTime('2018-01-02 00:06:00'), new \DateTime('2018-01-02 00:07:00') ); $calculator = new StdRangesCalculator(); $ranges = $calculator->sub( $range1, $range2 ); print_r($ranges); // prints out Array ( [0] => Hop\Ranges\Range Object ( [dateFrom:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-01 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC ) [dateTo:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-02 00:05:59.000000 [timezone_type] => 3 [timezone] => UTC ) ) [1] => Hop\Ranges\Range Object ( [dateFrom:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-02 00:07:01.000000 [timezone_type] => 3 [timezone] => UTC ) [dateTo:Hop\Ranges\Range:private] => DateTime Object ( [date] => 2018-01-05 00:05:00.000000 [timezone_type] => 3 [timezone] => UTC ) ) )
Enjoy!
© Tomasz Chmielewski