- end: documentation ( source)
<?php
// Everything you enter here will be executed by our servers. Try it!
class Contract
{
private $startDate;
private $numberofDays;
private $endDate;
public function __construct($startDate, $numberofDays)
{
$this->startDate = \DateTime::createFromFormat('Y-m-d', $startDate);
$this->numberofDays = $numberofDays;
$this->endDate = $this->getEndDate();
}
public function overLap(Contract $other)
{
$overlap = false;
if($other->getEndDate() < $this->startDate || $other->getStartDate() > $this->endDate){
$overlap = true;
}
return $overlap;
}
/**
* Given the start date of a contract will return the end date, skipping weekends
*
* @return Array of date strings
*/
public function getDeadline()
{
$start = (new \DateTime())->setTimestamp($this->startDate->getTimestamp());
$interval = new DateInterval('P1D');
$result = array();
$i = 0;
while($i < $this->numberofDays){
if((int)$start->format('N') < 6){
$result[] = $start->format('Y-m-d');
$i++;
}
$start->add($interval);
}
return $result;
}
public function getEndDate()
{
if(!$this->endDate){
$dates = $this->getDeadline();
$this->endDate = end($dates);
}
return $this->endDate;
}
public function getStartDate()
{
return $this->startDate;
}
}