DateHelper::convertToLocal()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Gameap\Helpers;
4
5
use Carbon\Carbon;
6
use Carbon\CarbonImmutable;
7
8
class DateHelper
9
{
10
    public static function convertToLocal(?Carbon $date): ?CarbonImmutable
11
    {
12
        if ($date === null) {
13
            return null;
14
        }
15
16
        $convertedDate = CarbonImmutable::createFromFormat(
17
            Carbon::DEFAULT_TO_STRING_FORMAT,
18
            $date->toDateTimeString(),
19
            'UTC'
20
        );
21
        return $convertedDate->setTimezone(config('timezone'));
22
    }
23
24
    public static function convertToUTC(?Carbon $date): ?CarbonImmutable
25
    {
26
        if ($date === null) {
27
            return null;
28
        }
29
30
        $convertedDate = CarbonImmutable::createFromFormat(
31
            Carbon::DEFAULT_TO_STRING_FORMAT,
32
            $date->toDateTimeString(),
33
            config('timezone')
34
        );
35
        return $convertedDate->setTimezone('UTC');
36
    }
37
}
38