Skip to content

Commit

Permalink
Add show_datetime_format(); optional default timezone parameter #741 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jun 4, 2020
1 parent 3d21153 commit 04b8be1
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit 04b8be1

Please sign in to comment.