firefly-iii/app/Support/Http/Controllers/DateCalculation.php

170 lines
5.0 KiB
PHP
Raw Normal View History

<?php
/**
* DateCalculation.php
2020-02-16 06:56:52 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
/**
* Trait DateCalculation
*
*/
trait DateCalculation
{
2018-07-14 16:22:08 -05:00
/**
2018-08-07 10:34:43 -05:00
* Calculate the number of days passed left until end date, as seen from start date.
* If today is between start and end, today will be used instead of end.
*
* If both are in the past OR both are in the future, simply return the number of days in the period with a minimum of 1
*
2022-12-29 12:42:26 -06:00
* @param Carbon $start
* @param Carbon $end
2018-07-14 16:22:08 -05:00
*
2018-08-07 10:34:43 -05:00
* @return int
2018-07-14 16:22:08 -05:00
*/
2018-08-07 10:34:43 -05:00
public function activeDaysLeft(Carbon $start, Carbon $end): int
2018-07-14 16:22:08 -05:00
{
2018-08-07 10:34:43 -05:00
$difference = $start->diffInDays($end) + 1;
2023-02-11 00:36:45 -06:00
$today = today(config('app.timezone'))->startOfDay();
2018-07-14 16:22:08 -05:00
2018-08-07 10:34:43 -05:00
if ($start->lte($today) && $end->gte($today)) {
$difference = $today->diffInDays($end);
2018-07-14 16:22:08 -05:00
}
2021-03-21 03:15:40 -05:00
2020-10-23 12:11:25 -05:00
return 0 === $difference ? 1 : $difference;
2018-07-14 16:22:08 -05:00
}
/**
2018-08-07 10:34:43 -05:00
* Calculate the number of days passed between two dates. Will take the current moment into consideration.
*
* If both are in the past OR both are in the future, simply return the period between them with a minimum of 1
*
2022-12-29 12:42:26 -06:00
* @param Carbon $start
* @param Carbon $end
*
* @return int
*/
2018-08-07 10:34:43 -05:00
protected function activeDaysPassed(Carbon $start, Carbon $end): int
{
2018-08-07 10:34:43 -05:00
$difference = $start->diffInDays($end) + 1;
2023-02-11 00:36:45 -06:00
$today = today(config('app.timezone'))->startOfDay();
2018-08-07 10:34:43 -05:00
if ($start->lte($today) && $end->gte($today)) {
$difference = $start->diffInDays($today) + 1;
}
2018-08-07 10:34:43 -05:00
return $difference;
}
/**
2022-12-29 12:42:26 -06:00
* @param Carbon $start
* @param Carbon $end
*
2018-08-07 10:34:43 -05:00
* @return string
*/
2018-08-07 10:34:43 -05:00
protected function calculateStep(Carbon $start, Carbon $end): string
{
2018-08-07 10:34:43 -05:00
$step = '1D';
$months = $start->diffInMonths($end);
if ($months > 3) {
$step = '1W';
}
2018-08-07 10:34:43 -05:00
if ($months > 24) {
$step = '1M';
2018-08-07 10:34:43 -05:00
}
if ($months > 100) {
$step = '1Y';
}
2018-08-07 10:34:43 -05:00
return $step;
}
2018-07-14 10:23:44 -05:00
/**
* Get a list of the periods that will occur after this date. For example,
* March 2018, April 2018, etc.
*
2022-12-29 12:42:26 -06:00
* @param Carbon $date
* @param string $range
2018-07-14 10:23:44 -05:00
*
* @return array
*/
protected function getNextPeriods(Carbon $date, string $range): array
{
// select thing for next 12 periods:
$loop = [];
/** @var Carbon $current */
$current = app('navigation')->startOfPeriod($date, $range);
$current = app('navigation')->endOfPeriod($current, $range);
2018-07-14 10:23:44 -05:00
$current->addDay();
$count = 0;
while ($count < 12) {
$current = app('navigation')->endOfPeriod($current, $range);
$currentStart = app('navigation')->startOfPeriod($current, $range);
$loop[] = [
'label' => $current->format('Y-m-d'),
'title' => app('navigation')->periodShow($current, $range),
'start' => clone $currentStart,
'end' => clone $current,
];
2018-07-14 10:23:44 -05:00
++$count;
$current->addDay();
}
return $loop;
}
/**
* Get a list of the periods that occurred before the start date. For example,
* March 2018, February 2018, etc.
*
2022-12-29 12:42:26 -06:00
* @param Carbon $date
* @param string $range
2018-07-14 10:23:44 -05:00
*
* @return array
*/
protected function getPreviousPeriods(Carbon $date, string $range): array
{
// select thing for last 12 periods:
$loop = [];
/** @var Carbon $current */
$current = app('navigation')->startOfPeriod($date, $range);
2018-07-14 10:23:44 -05:00
$count = 0;
while ($count < 12) {
$current->subDay();
$current = app('navigation')->startOfPeriod($current, $range);
$currentEnd = app('navigation')->endOfPeriod($current, $range);
$loop[] = [
'label' => $current->format('Y-m-d'),
'title' => app('navigation')->periodShow($current, $range),
'start' => clone $current,
'end' => clone $currentEnd,
];
2018-07-14 10:23:44 -05:00
++$count;
}
return $loop;
}
}