From 04b8be1649e00fc8d699f3c3e7772f0e28fe2c84 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Thu, 4 Jun 2020 08:34:30 -0700 Subject: [PATCH] Add show_datetime_format(); optional default timezone parameter #741 (#746) --- app/helpers.php | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/app/helpers.php b/app/helpers.php index 48798da7b..9a598c800 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -4,6 +4,7 @@ use App\Repositories\SettingRepository; use Carbon\Carbon; use Illuminate\Contracts\View\Factory; +use Illuminate\Support\Facades\Auth; /* * array_key_first only exists in PHP 7.3+ @@ -266,12 +267,13 @@ function show_datetime(Carbon $date = null) * but convert it into the user's timezone * * @param \Carbon\Carbon $date + * @param string $default_timezone Default timezone to use, defaults to UTC * * @return string */ - function show_date(Carbon $date) + function show_date(Carbon $date, $default_timezone = 'UTC') { - $timezone = 'UTC'; + $timezone = $default_timezone; if (Auth::check()) { $timezone = Auth::user()->timezone ?: $timezone; } @@ -280,6 +282,31 @@ function show_date(Carbon $date) } } +/* + * Show a date/time in the proper timezone for a user + */ +if (!function_exists('show_datetime_format')) { + /** + * Format the a Carbon date into the datetime string + * but convert it into the user's timezone + * + * @param \Carbon\Carbon $date + * @param string $format + * @param string $default_timezone A default timezone to use (UTC by default) + * + * @return string + */ + function show_datetime_format(Carbon $date, $format, $default_timezone = 'UTC') + { + $timezone = $default_timezone; + if (Auth::check()) { + $timezone = Auth::user()->timezone ?: $timezone; + } + + return $date->timezone($timezone)->format($format); + } +} + if (!function_exists('_fmt')) { /** * Replace strings