Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created October 24, 2019 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/03f9e69a6373b1d093a98e7ce56bcabd to your computer and use it in GitHub Desktop.
Save kobus1998/03f9e69a6373b1d093a98e7ce56bcabd to your computer and use it in GitHub Desktop.
added functionality to period
<?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