PHP code example of sportfinder / time

1. Go to this page and download the library: Download sportfinder/time 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/ */

    

sportfinder / time example snippets


namespace SportFinder\Time;

interface DateSlotableInterface
{
    public function getStart(): ?\DateTime;
    public function getEnd(): ?\DateTime;
    public function toDateSlot(): DateSlotInterface;
}

namespace SportFinder\Time;

interface DateSlotInterface extends DateSlotableInterface
{
    public function contains($dateTimeOrDateSlot, $openLeft = false, $openRight = false): bool;
    public function equals(DateSlotInterface $dateSlot): bool;
    public function intersect(DateSlotableInterface $interval = null);
    public function subtract($dateSlot);
    public function getDuration($unit = Units::SECOND): int;
    public function hasTimeLeft(): bool;
    public function sub(\DateInterval $interval);
    public function add(\DateInterval $interval);
}

namespace SportFinder\Time;

interface ComparatorInterface
{
    public function isBefore($dateTimeOrDateSlot, $intervalOpen = false): bool;
    public function isAfter($dateTimeOrDateSlot, $intervalOpen = false): bool;
}

namespace SportFinder\Time;

class DateSlot implements DateSlotInterface, DurationInterface, ComparatorInterface{}

namespace SportFinder\Time;

class DateTime extends \DateTime implements ComparatorInterface, DateSlotableInterface{}

$from = \DateTime::createFromFormat('Y-m-d', '2020-01-01');
$to = \DateTime::createFromFormat('Y-m-d', '2020-01-31');
$dateSlot1 = new DateSlot($from, $to);
$dateSlot2 = new DateSlot($from, $to);
$dateSlot1 == $dateSlot2; // true

$dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-01'), \DateTime::createFromFormat('Y-m-d', '2020-01-02'));
$dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-02'), \DateTime::createFromFormat('Y-m-d', '2020-01-03'));
$dateSlot1 == $dateSlot2; // false
$dateSlot1->add(new \DateInterval('P1D'));
$dateSlot1 == $dateSlot2; // true

// [2020-01-01, 2020-01-02]
$dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-01'), \DateTime::createFromFormat('Y-m-d', '2020-01-02'));
// [2020-01-03, 2020-01-04]
$dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-03'), \DateTime::createFromFormat('Y-m-d', '2020-01-04'));
$dateSlot1->isBefore($dateSlot2); // true
$dateSlot2->isAfter($dateSlot1); // true

// [2020-01-01, 2020-01-02]
$dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-01'), \DateTime::createFromFormat('Y-m-d', '2020-01-02'));
// [2020-01-02, 2020-01-03]
$dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-02'), \DateTime::createFromFormat('Y-m-d', '2020-01-03'));
$dateSlot1->isBefore($dateSlot2); // [2020-01-01, 2020-01-02] < [2020-01-02, 2020-01-03] ? false
$dateSlot1->isBefore($dateSlot2, true); // [2020-01-01, 2020-01-02[ < ]2020-01-02, 2020-01-03] ? true

$dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-01-31'));
$dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '1999-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-01-31'));
$intersect = $dateSlot1->intersect($dateSlot2);
$expected = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-01-31'));
$intersect == $expected; // True

$year2000 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-12-31'));
$february2000 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-02-01'), \DateTime::createFromFormat('Y-m-d', '2000-03-01'));
$november2000 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-11-01'), \DateTime::createFromFormat('Y-m-d', '2000-12-01'));
$year2000->subtract($february2000); // [[2000-01-01, 2000-02-01], [2000-03-01, 2000-12-31]]
$year2000->subtract([$february2000, $november2000]); // [[2000-01-01, 2000-02-01], [2000-03-01, 2000-11-01], [2000-12-01, 2000-12-31]]