Files
firefly-iii/app/lib/FireflyIII/Shared/Toolkit/Date.php

272 lines
7.5 KiB
PHP
Raw Normal View History

2014-10-29 10:30:52 +01:00
<?php
namespace FireflyIII\Shared\Toolkit;
2014-11-13 16:13:32 +01:00
use FireflyIII\Exception\FireflyException;
2014-10-29 10:30:52 +01:00
/**
* Class Date
*
* @package FireflyIII\Shared\Toolkit
*/
class Date
{
/**
2014-12-25 09:50:01 +01:00
* @param \Carbon\Carbon $theDate
* @param $repeatFreq
* @param $skip
2014-10-29 10:30:52 +01:00
*
2014-12-25 09:50:01 +01:00
* @return \Carbon\Carbon
2014-10-29 10:30:52 +01:00
* @throws FireflyException
*/
2014-12-25 09:50:01 +01:00
public function addPeriod(\Carbon\Carbon $theDate, $repeatFreq, $skip)
2014-10-29 10:30:52 +01:00
{
2014-11-21 19:18:53 +01:00
$date = clone $theDate;
$add = ($skip + 1);
2014-12-14 20:40:02 +01:00
2014-10-29 10:30:52 +01:00
switch ($repeatFreq) {
default:
2014-11-12 22:37:09 +01:00
throw new FireflyException('Cannot do addPeriod for $repeat_freq ' . $repeatFreq);
2014-10-29 10:30:52 +01:00
break;
case 'daily':
2014-11-12 22:37:09 +01:00
$date->addDays($add);
2014-10-29 10:30:52 +01:00
break;
2014-11-15 11:36:27 +01:00
case 'week':
2014-10-29 10:30:52 +01:00
case 'weekly':
2014-11-12 22:37:09 +01:00
$date->addWeeks($add);
2014-10-29 10:30:52 +01:00
break;
2014-11-15 11:36:27 +01:00
case 'month':
2014-10-29 10:30:52 +01:00
case 'monthly':
2014-11-12 22:37:09 +01:00
$date->addMonths($add);
2014-10-29 10:30:52 +01:00
break;
case 'quarter':
2014-10-29 10:30:52 +01:00
case 'quarterly':
2014-11-12 22:37:09 +01:00
$months = $add * 3;
$date->addMonths($months);
2014-10-29 10:30:52 +01:00
break;
case 'half-year':
2014-11-12 22:37:09 +01:00
$months = $add * 6;
$date->addMonths($months);
2014-10-29 10:30:52 +01:00
break;
case 'year':
2014-10-29 10:30:52 +01:00
case 'yearly':
2014-11-12 22:37:09 +01:00
$date->addYears($add);
2014-10-29 10:30:52 +01:00
break;
}
2014-11-12 22:37:09 +01:00
return $date;
2014-10-29 10:30:52 +01:00
}
/**
2014-12-25 09:50:01 +01:00
* @param \Carbon\Carbon $theCurrentEnd
* @param $repeatFreq
2014-10-29 10:30:52 +01:00
*
2014-12-25 09:50:01 +01:00
* @return \Carbon\Carbon
2014-10-29 10:30:52 +01:00
* @throws FireflyException
*/
2014-12-25 09:50:01 +01:00
public function endOfPeriod(\Carbon\Carbon $theCurrentEnd, $repeatFreq)
2014-10-29 10:30:52 +01:00
{
2014-11-21 19:18:53 +01:00
$currentEnd = clone $theCurrentEnd;
2014-10-29 10:30:52 +01:00
switch ($repeatFreq) {
default:
2014-11-12 22:37:09 +01:00
throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq);
2014-10-29 10:30:52 +01:00
break;
case 'daily':
2014-11-12 22:37:09 +01:00
$currentEnd->addDay();
2014-10-29 10:30:52 +01:00
break;
case 'week':
2014-10-29 10:30:52 +01:00
case 'weekly':
2014-11-12 22:37:09 +01:00
$currentEnd->addWeek()->subDay();
2014-10-29 10:30:52 +01:00
break;
2014-11-17 23:09:52 +01:00
case 'month':
2014-10-29 10:30:52 +01:00
case 'monthly':
2014-11-12 22:37:09 +01:00
$currentEnd->addMonth()->subDay();
2014-10-29 10:30:52 +01:00
break;
2014-11-24 10:12:34 +01:00
case 'quarter':
2014-10-29 10:30:52 +01:00
case 'quarterly':
2014-11-12 22:37:09 +01:00
$currentEnd->addMonths(3)->subDay();
2014-10-29 10:30:52 +01:00
break;
case 'half-year':
2014-11-12 22:37:09 +01:00
$currentEnd->addMonths(6)->subDay();
2014-10-29 10:30:52 +01:00
break;
2014-11-24 10:12:34 +01:00
case 'year':
2014-10-29 10:30:52 +01:00
case 'yearly':
2014-11-12 22:37:09 +01:00
$currentEnd->addYear()->subDay();
2014-10-29 10:30:52 +01:00
break;
}
2014-11-17 07:33:18 +01:00
2014-11-15 07:46:01 +01:00
return $currentEnd;
2014-10-29 10:30:52 +01:00
}
2014-11-13 16:13:32 +01:00
2014-11-24 17:01:37 +01:00
/**
2014-12-25 09:50:01 +01:00
* @param \Carbon\Carbon $theCurrentEnd
* @param $repeatFreq
* @param \Carbon\Carbon $maxDate
2014-11-24 17:01:37 +01:00
*
2014-12-25 09:50:01 +01:00
* @return \Carbon\Carbon
2014-11-24 17:01:37 +01:00
* @throws FireflyException
*/
2014-12-25 09:50:01 +01:00
public function endOfX(\Carbon\Carbon $theCurrentEnd, $repeatFreq, \Carbon\Carbon $maxDate)
2014-11-24 17:01:37 +01:00
{
$currentEnd = clone $theCurrentEnd;
switch ($repeatFreq) {
default:
throw new FireflyException('Cannot do endOfPeriod for $repeat_freq ' . $repeatFreq);
break;
case 'daily':
$currentEnd->endOfDay();
break;
case 'week':
case 'weekly':
$currentEnd->endOfWeek();
break;
case 'month':
case 'monthly':
$currentEnd->endOfMonth();
break;
case 'quarter':
case 'quarterly':
$currentEnd->lastOfQuarter();
break;
case 'half-year':
$month = intval($theCurrentEnd->format('m'));
$currentEnd->endOfYear();
2014-12-13 23:40:25 +01:00
if ($month <= 6) {
2014-11-24 17:01:37 +01:00
$currentEnd->subMonths(6);
}
break;
case 'year':
case 'yearly':
$currentEnd->endOfYear();
break;
}
2014-12-14 20:40:02 +01:00
if ($currentEnd > $maxDate) {
return clone $maxDate;
}
2014-11-24 17:01:37 +01:00
return $currentEnd;
}
2014-12-13 21:59:02 +01:00
/**
2014-12-25 09:50:01 +01:00
* @param \Carbon\Carbon $date
* @param $repeatFrequency
2014-12-13 21:59:02 +01:00
*
* @return string
* @throws FireflyException
*/
2014-12-25 09:50:01 +01:00
public function periodShow(\Carbon\Carbon $date, $repeatFrequency)
{
switch ($repeatFrequency) {
default:
throw new FireflyException('No date formats for frequency "' . $repeatFrequency . '"!');
break;
case 'daily':
return $date->format('j F Y');
break;
2014-11-24 17:01:37 +01:00
case 'week':
case 'weekly':
return $date->format('\W\e\e\k W, Y');
break;
2014-11-24 17:01:37 +01:00
case 'quarter':
return $date->format('F Y');
break;
case 'monthly':
case 'month':
return $date->format('F Y');
break;
2014-11-24 17:01:37 +01:00
case 'year':
case 'yearly':
return $date->format('Y');
break;
}
}
2014-11-13 16:13:32 +01:00
/**
2014-12-25 09:50:01 +01:00
* @param \Carbon\Carbon $theDate
* @param $repeatFreq
2014-11-13 16:13:32 +01:00
*
2014-12-25 09:50:01 +01:00
* @return \Carbon\Carbon
2014-11-13 16:13:32 +01:00
* @throws FireflyException
*/
2014-12-25 09:50:01 +01:00
public function startOfPeriod(\Carbon\Carbon $theDate, $repeatFreq)
2014-11-13 16:13:32 +01:00
{
2014-11-21 19:18:53 +01:00
$date = clone $theDate;
2014-11-13 16:13:32 +01:00
switch ($repeatFreq) {
default:
throw new FireflyException('Cannot do startOfPeriod for $repeat_freq ' . $repeatFreq);
break;
case 'daily':
$date->startOfDay();
break;
case 'week':
2014-11-13 16:13:32 +01:00
case 'weekly':
$date->startOfWeek();
break;
2014-11-17 23:09:52 +01:00
case 'month':
2014-11-13 16:13:32 +01:00
case 'monthly':
$date->startOfMonth();
break;
case 'quarter':
2014-11-13 16:13:32 +01:00
case 'quarterly':
$date->firstOfQuarter();
break;
case 'half-year':
$month = intval($date->format('m'));
$date->startOfYear();
if ($month >= 7) {
$date->addMonths(6);
}
break;
2014-11-24 10:12:34 +01:00
case 'year':
2014-11-13 16:13:32 +01:00
case 'yearly':
$date->startOfYear();
break;
}
return $date;
}
/**
2014-12-25 09:50:01 +01:00
* @param \Carbon\Carbon $theDate
* @param $repeatFreq
* @param int $subtract
*
2014-12-25 09:50:01 +01:00
* @return \Carbon\Carbon
* @throws FireflyException
*/
2014-12-25 09:50:01 +01:00
public function subtractPeriod(\Carbon\Carbon $theDate, $repeatFreq, $subtract = 1)
{
$date = clone $theDate;
switch ($repeatFreq) {
default:
throw new FireflyException('Cannot do subtractPeriod for $repeat_freq ' . $repeatFreq);
break;
case 'daily':
$date->subDays($subtract);
break;
case 'weekly':
$date->subWeeks($subtract);
break;
case 'monthly':
$date->subMonths($subtract);
break;
case 'quarter':
case 'quarterly':
$months = $subtract * 3;
$date->subMonths($months);
break;
case 'half-year':
$months = $subtract * 6;
$date->subMonths($months);
break;
case 'year':
case 'yearly':
$date->subYears($subtract);
break;
}
return $date;
}
}