Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show_datetime_format() helper; optional default timezone parameter #741 #746

Merged
merged 2 commits into from
Jun 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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