Files
pathfinder/app/main/lib/DateRange.php

55 lines
1.1 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Exodus 4D
* Date: 29.11.2018
* Time: 18:06
*/
namespace lib;
use Exception\DateException;
class DateRange {
/**
* from range start
* @var \DateTime
*/
protected $from;
/**
* to range end
* @var \DateTime
*/
protected $to;
/**
* DateRange constructor.
* @param \DateTime $from
* @param \DateTime $to
* @throws DateException
*/
public function __construct(\DateTime $from, \DateTime $to){
if($from == $to){
throw new DateException('A period cannot be the same time', 3000);
}else{
if($from < $to){
$this->from = $from;
$this->to = $to;
} else {
$this->from = $to;
$this->to = $from;
}
}
}
/**
* check if DateTime $dateCheck is within this range
* @param \DateTime $dateCheck
* @return bool
*/
public function inRange(\DateTime $dateCheck) : bool {
return $dateCheck >= $this->from && $dateCheck <= $this->to;
}
}