/
DateFormatter.php
45 lines (38 loc) · 1.04 KB
/
DateFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
declare(strict_types=1);
namespace MichaelRubel\Formatters\Collection;
use Carbon\CarbonInterface;
use Illuminate\Support\Carbon;
use MichaelRubel\Formatters\Formatter;
class DateFormatter implements Formatter
{
/**
* @param string|CarbonInterface|null $date
* @param string|null $timezone
* @param string|null $date_format
*/
public function __construct(
public string|null|CarbonInterface $date = null,
public ?string $timezone = null,
public ?string $date_format = 'Y-m-d',
) {
if (empty($this->date)) {
$this->date = null;
}
if (is_string($this->date)) {
$this->date = app(Carbon::class)->parse($this->date);
}
$this->timezone ??= config('app.timezone', 'UTC');
}
/**
* Format the date.
*
* @return string|null
*/
public function format(): ?string
{
return $this->date
?->setTimezone($this->timezone)
->translatedFormat($this->date_format);
}
}