From 2d9eb8640b8e259651743b679be1b36651fcac7f Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Fri, 20 May 2022 13:52:42 -0600 Subject: [PATCH] wip: a macro to extend Carbon\Carbon with a nice interval --- src/app/Providers/CarbonServiceProvider.php | 76 +++++++++++++++++++++ src/config/app.php | 22 ++++++ 2 files changed, 98 insertions(+) create mode 100644 src/app/Providers/CarbonServiceProvider.php create mode 100644 src/config/app.php diff --git a/src/app/Providers/CarbonServiceProvider.php b/src/app/Providers/CarbonServiceProvider.php new file mode 100644 index 0000000..68d998c --- /dev/null +++ b/src/app/Providers/CarbonServiceProvider.php @@ -0,0 +1,76 @@ +tz); + } + + $diffInMinutes = $this->diffInMinutes($other); + + $minutes = $diffInMinutes % 60; + + $diffInHours = $diffInMinutes / 60; + $hours = $diffInHours % 60; + + $diffInDays = $diffInHours / 24; + $days = $diffInDays % 24; + + $diffInWeeks = $diffInDays / 24; + $weeks = $diffInWeeks % 24; + + $diffInMonths = $diffInWeeks / 7; + $months = $diffInMonths % 7; + + $diffInYears = $diffInMonths / 12; + $years = $diffInYears % 12; + + $outputArray = []; + if ($years > 0) { + $outputArray['years'] = "$years years"; + } + if ($months > 0) { + $outputArray['months'] = "$months months"; + } + if ($weeks > 0 && $days === 0) { + $outputArray['weeks'] = "$weeks weeks"; + } + if ($days > 0) { + $outputArray['days'] = "$days days"; + } + if ($hours > 0) { + $outputArray['hours'] = "$hours hours"; + } + if ($minutes > 0) { + $outputArray['minutes'] = "$minutes minutes"; + } + + return implode(', ', $outputArray); + }); + } +} diff --git a/src/config/app.php b/src/config/app.php new file mode 100644 index 0000000..5046d7e --- /dev/null +++ b/src/config/app.php @@ -0,0 +1,22 @@ + [ + + ... + + /* + * Application Service Providers... + */ + ... + App\Providers\CarbonServiceProvider::class, + ], + + ... + +];