Created
October 24, 2019 07:49
-
-
Save kobus1998/03f9e69a6373b1d093a98e7ce56bcabd to your computer and use it in GitHub Desktop.
added functionality to period
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ComplexPeriod extends DatePeriod | |
{ | |
public function __construct($start, $end) | |
{ | |
parent::__construct( | |
new \DateTime(date("Y-m-d", strtotime($start))), | |
new \DateInterval('P1D'), | |
new \DateTime(date("Y-m-d", strtotime($end))) | |
); | |
} | |
public function toArray($format = 'Y-m-d') | |
{ | |
$result = []; | |
foreach($this as $date) { | |
$result[] = $date->format($format); | |
} | |
return $result; | |
} | |
public function diff() | |
{ | |
return $this->getStartDate()->diff($this->getEndDate()); | |
} | |
public function overlaps(\DatePeriod $period) | |
{ | |
$start1 = $this->getStartDate()->format('Ymd'); | |
$end1 = $this->getEndDate()->format('Ymd'); | |
$start2 = $period->getStartDate()->format('Ymd'); | |
$end2 = $period->getEndDate()->format('Ymd'); | |
return $start1 < $start2 || $end1 > $end2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment