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

139 lines
3.6 KiB
PHP
Raw Normal View History

2014-10-29 10:30:52 +01:00
<?php
namespace FireflyIII\Shared\Toolkit;
use Carbon\Carbon;
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-11-21 19:18:53 +01:00
* @param Carbon $theDate
2014-10-29 10:30:52 +01:00
* @param $repeatFreq
2014-11-12 22:37:09 +01:00
* @param $skip
2014-10-29 10:30:52 +01:00
*
2014-11-12 22:37:09 +01:00
* @return Carbon
2014-10-29 10:30:52 +01:00
* @throws FireflyException
*/
2014-11-21 19:18:53 +01:00
public function addPeriod(Carbon $theDate, $repeatFreq, $skip)
2014-10-29 10:30:52 +01:00
{
2014-11-21 19:18:53 +01:00
$date = clone $theDate;
2014-11-12 22:37:09 +01:00
$add = ($skip + 1);
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 '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 '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-11-21 19:18:53 +01:00
* @param Carbon $theCurrentEnd
2014-10-29 10:30:52 +01:00
* @param $repeatFreq
*
2014-11-21 19:18:53 +01:00
* @return mixed
2014-10-29 10:30:52 +01:00
* @throws FireflyException
*/
2014-11-21 19:18:53 +01:00
public function endOfPeriod(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;
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;
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-21 19:18:53 +01:00
* @param Carbon $theDate
2014-11-13 16:13:32 +01:00
* @param $repeatFreq
*
* @return Carbon
* @throws FireflyException
*/
2014-11-21 19:18:53 +01:00
public function startOfPeriod(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 'quarterly':
$date->firstOfQuarter();
break;
case 'half-year':
$month = intval($date->format('m'));
$date->startOfYear();
if ($month >= 7) {
$date->addMonths(6);
}
break;
case 'yearly':
$date->startOfYear();
break;
}
return $date;
}
2014-11-21 19:18:53 +01:00
}