PHP code example of short-edition / interval
1. Go to this page and download the library: Download short-edition/interval library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
short-edition / interval example snippets
$interval = new Interval(20, 40);// [20, 40];
$interval = Interval::create('[20,40]');// [20, 40];
echo $interval->intersect(new Interval(30, 60)); // [30, 40];
echo $interval->union(new Interval(30, 60)); // {[20, 60]};
echo $interval->union(new Interval(60, 100)); // {[20, 40], [60, 100]};
echo $interval->exclude(new Interval(30, 60)); // {[20, 30[};
echo $interval->exclude(new Interval(30, 35)); // {[20, 30[, ]35, 40]};
echo $interval->overlaps(new Interval(30, 60)); // true;
echo $interval->
$interval = new Interval(new \DateTime('2016-01-01'), new \DateTime('2016-01-10'));
// [2016-01-01T00:00:00+01:00, 2016-01-10T00:00:00+01:00];
echo $interval->union(Interval::create('[2016-01-10, 2016-01-15]'));
// {[2016-01-01T00:00:00+01:00, 2016-01-15T00:00:00+01:00]};
$interval = new Interval(-INF, INF);// ]-∞, +∞[;
echo $interval->exclude(Interval::create('[2016-01-10, 2016-01-15]'));
// {]-∞, 2016-01-10T00:00:00+01:00[, ]2016-01-15T00:00:00+01:00, +∞[};
$intervals = Intervals::create(['[0,5]', '[8,12]']);// {[0, 5], [8, 12]};
echo $intervals->exclude(Intervals::create(['[3,10]'])); // {[0, 3[, ]10, 12]};
$result = Interval
::create('[10, 20]')
->intersect(new Interval(11, 30))
->union(new Interval(15, INF))
->exclude(Intervals::create(['[18, 20]', '[25, 30]', '[32, 35]', '[12, 13]']))
->sort(function (Interval $first, Interval $second) {
return $first->getStart()->getValue() <=> $second->getStart()->getValue();
})
->map(function (Interval $interval) {
return new Interval(
$interval->getStart()->getValue() ** 2,
$interval->getEnd()->getValue() ** 2
);
})
->filter(function (Interval $interval) {
return $interval->getEnd()->getValue() > 170;
});
// {[169, 324], [400, 625], [900, 1024], [1225, +∞[};
echo $result;
$result = Intervals
::create([']10, +INF['])
->exclude(Intervals::create([']18, 20]', ']25, 30[', '[32, 35]', ']12, 13]']));
// {]10, 12], ]13, 18], ]20, 25], [30, 32[, ]35, +∞[}