80 lines
2.0 KiB
PHP
80 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Leenooks;
|
|
|
|
use Carbon\Carbon as CarbonBase;
|
|
|
|
/**
|
|
* This overrides the default Carbon\Carbon class so that we can enable
|
|
* additional Date functions, like Half Years
|
|
*/
|
|
|
|
class Carbon extends CarbonBase
|
|
{
|
|
const MONTHS_PER_HALF = 6;
|
|
|
|
public function __get($name)
|
|
{
|
|
switch ($name) {
|
|
case 'half':
|
|
return (int) ceil($this->month / static::MONTHS_PER_HALF);
|
|
|
|
case 'quarter_string':
|
|
return sprintf('%dQ%d',$this->year,$this->quarter);
|
|
|
|
default:
|
|
return parent::__get($name);
|
|
}
|
|
}
|
|
|
|
public function addHalf(): self
|
|
{
|
|
return $this->addHalves(1);
|
|
}
|
|
|
|
public function addHalves(int $unit): self
|
|
{
|
|
return $this->addQuarters(2*$unit);
|
|
}
|
|
|
|
public function subHalf(): self
|
|
{
|
|
return $this->subHalves(1);
|
|
}
|
|
|
|
public function subHalves(int $unit): self
|
|
{
|
|
return $this->subQuarters(2*$unit);
|
|
}
|
|
|
|
/**
|
|
* Modify to the first occurrence of a given day of the week
|
|
* in the current quarter. If no dayOfWeek is provided, modify to the
|
|
* first day of the current quarter. Use the supplied constants
|
|
* to indicate the desired dayOfWeek, ex. static::MONDAY.
|
|
*
|
|
* @param int|null $dayOfWeek
|
|
*
|
|
* @return static
|
|
*/
|
|
public function startOfHalf($dayOfWeek = null)
|
|
{
|
|
return $this->setDate($this->year, $this->half * static::MONTHS_PER_HALF - 5, 1)->firstOfMonth($dayOfWeek);
|
|
}
|
|
|
|
/**
|
|
* Modify to the last occurrence of a given day of the week
|
|
* in the current quarter. If no dayOfWeek is provided, modify to the
|
|
* last day of the current quarter. Use the supplied constants
|
|
* to indicate the desired dayOfWeek, ex. static::MONDAY.
|
|
*
|
|
* @param int|null $dayOfWeek
|
|
*
|
|
* @return static
|
|
*/
|
|
public function endOfHalf($dayOfWeek = null)
|
|
{
|
|
return $this->setDate($this->year, $this->half * static::MONTHS_PER_HALF, 1)->lastOfMonth($dayOfWeek);
|
|
}
|
|
}
|