. */ declare(strict_types=1); namespace FireflyIII\Support\Http\Controllers; use Carbon\Carbon; use Log; /** * Trait DateCalculation * * @package FireflyIII\Support\Http\Controllers */ trait DateCalculation { /** * Returns the number of days between the two given dates. * - If today is within the two dates, give the number of days between today and the end date. * - If they are the same, return 1. * * @param Carbon $start * @param Carbon $end * * @return int */ protected function getDayDifference(Carbon $start, Carbon $end): int { $dayDifference = 0; // if today is between start and end, use the diff in days between end and today (days left) // otherwise, use diff between start and end. $today = new Carbon; Log::debug(sprintf('Start is %s, end is %s, today is %s', $start->format('Y-m-d'), $end->format('Y-m-d'), $today->format('Y-m-d'))); if ($today->gte($start) && $today->lte($end)) { $dayDifference = $end->diffInDays($today); } if ($today->lte($start) || $today->gte($end)) { $dayDifference = $start->diffInDays($end); } $dayDifference = 0 === $dayDifference ? 1 : $dayDifference; return $dayDifference; } /** * Returns the number of days that have passed in this period. If it is zero (start of period) * then return 1. * * @param Carbon $start * @param Carbon $end * * @return int */ protected function getDaysPassedInPeriod(Carbon $start, Carbon $end): int { // if today is between start and end, use the diff in days between end and today (days left) // otherwise, use diff between start and end. $today = new Carbon; $daysPassed = 0; Log::debug(sprintf('Start is %s, end is %s, today is %s', $start->format('Y-m-d'), $end->format('Y-m-d'), $today->format('Y-m-d'))); if ($today->gte($start) && $today->lte($end)) { $daysPassed = $start->diffInDays($today); } if ($today->lte($start) || $today->gte($end)) { $daysPassed = $start->diffInDays($end); } $daysPassed = 0 === $daysPassed ? 1 : $daysPassed; return $daysPassed; } }