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

146 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* DateCalculation.php
2020-02-16 13:56:52 +01: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 23:22:08 +02:00
/**
2018-08-07 17:34:43 +02: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.
*
2023-06-21 12:34:58 +02:00
* 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
2018-07-14 23:22:08 +02:00
*/
2018-08-07 17:34:43 +02:00
public function activeDaysLeft(Carbon $start, Carbon $end): int
2018-07-14 23:22:08 +02:00
{
2024-12-22 08:43:12 +01:00
$difference = (int) ($start->diffInDays($end, true) + 1);
2023-02-11 07:36:45 +01:00
$today = today(config('app.timezone'))->startOfDay();
2018-08-07 17:34:43 +02:00
if ($start->lte($today) && $end->gte($today)) {
$difference = $today->diffInDays($end) + 1;
2018-07-14 23:22:08 +02:00
}
2021-03-21 09:15:40 +01:00
2024-12-22 08:43:12 +01:00
return (int) (0 === $difference ? 1 : $difference);
2018-07-14 23:22:08 +02:00
}
/**
2018-08-07 17:34:43 +02: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
*/
2018-08-07 17:34:43 +02:00
protected function activeDaysPassed(Carbon $start, Carbon $end): int
{
2024-03-17 12:00:28 +01:00
$difference = $start->diffInDays($end, true) + 1;
2023-02-11 07:36:45 +01:00
$today = today(config('app.timezone'))->startOfDay();
2018-08-07 17:34:43 +02:00
if ($start->lte($today) && $end->gte($today)) {
2024-03-17 12:00:28 +01:00
$difference = $start->diffInDays($today, true) + 1;
}
2024-12-22 08:43:12 +01:00
return (int) $difference;
}
2018-08-07 17:34:43 +02:00
protected function calculateStep(Carbon $start, Carbon $end): string
{
2018-08-07 17:34:43 +02:00
$step = '1D';
2024-03-17 12:00:28 +01:00
$months = $start->diffInMonths($end, true);
2018-08-07 17:34:43 +02:00
if ($months > 3) {
$step = '1W';
}
2018-08-07 17:34:43 +02:00
if ($months > 24) {
$step = '1M';
2018-08-07 17:34:43 +02:00
}
if ($months > 100) {
$step = '1Y';
}
2018-08-07 17:34:43 +02:00
return $step;
}
2018-07-14 17:23:44 +02:00
/**
* Get a list of the periods that will occur after this date. For example,
* March 2018, April 2018, etc.
*/
protected function getNextPeriods(Carbon $date, string $range): array
{
// select thing for next 12 periods:
$loop = [];
2023-12-20 19:35:52 +01:00
2018-07-14 17:23:44 +02:00
/** @var Carbon $current */
$current = app('navigation')->startOfPeriod($date, $range);
$current = app('navigation')->endOfPeriod($current, $range);
2018-07-14 17:23:44 +02:00
$current->addDay();
$count = 0;
2018-07-14 17:23:44 +02:00
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 17:23:44 +02: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.
*/
protected function getPreviousPeriods(Carbon $date, string $range): array
{
// select thing for last 12 periods:
$loop = [];
2023-12-20 19:35:52 +01:00
2018-07-14 17:23:44 +02:00
/** @var Carbon $current */
$current = app('navigation')->startOfPeriod($date, $range);
2018-07-14 17:23:44 +02: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 17:23:44 +02:00
++$count;
}
return $loop;
}
}