Skip to content

Commit

Permalink
Added the ability to set the timezone to UTC in the DateTime::prepare…
Browse files Browse the repository at this point in the history
…ForDatabase method
  • Loading branch information
simba77 committed Feb 18, 2023
1 parent 0e7e2df commit d72a3a7
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions system/src/Utility/DateTime.php
Expand Up @@ -46,13 +46,14 @@ public static function format(mixed $date, bool $withoutTime = false, bool $with
if (empty($date)) {
return '';
}
$siteSettings = di(SiteSettings::class);
try {
$siteSettings = di(SiteSettings::class);
if (is_integer($date)) {
$date_object = Carbon::createFromTimestamp($date, $siteSettings->getTimezone());
} else {
$date_object = Carbon::make($date)->timezone($siteSettings->getTimezone());
}

if ($date_object) {
if ($withoutTime) {
return $date_object->format('d.m.Y');
Expand Down Expand Up @@ -98,8 +99,12 @@ public static function calendarFormat(mixed $time)
/**
* Format for database
*/
public static function prepareForDatabase(mixed $time): string
public static function prepareForDatabase(mixed $time, bool $useTimezone = true): string
{
if ($useTimezone) {
$siteSettings = di(SiteSettings::class);
return Carbon::parse($time, $siteSettings->getTimezone())->setTimezone('UTC')->toDateTimeString();
}
return Carbon::parse($time)->toDateTimeString();
}
}

0 comments on commit d72a3a7

Please sign in to comment.