1 | <?php |
||
15 | class DateTime extends AbstractFormatterProvider |
||
16 | { |
||
17 | protected $carbon; |
||
18 | |||
19 | 12 | public function __construct(Carbon $carbon) |
|
23 | |||
24 | /** |
||
25 | * Format the datetime as a date |
||
26 | * |
||
27 | * @param mixed $value |
||
28 | * @param string $format |
||
29 | * @return string |
||
30 | */ |
||
31 | 3 | public function asDate($value, $format = 'Y-m-d') |
|
35 | |||
36 | /** |
||
37 | * Format the datetime as a date |
||
38 | * |
||
39 | * @param mixed $value |
||
40 | * @param string $format |
||
41 | * @return string |
||
42 | */ |
||
43 | 1 | public function asTime($value, $format = 'H:i:s') |
|
47 | |||
48 | /** |
||
49 | * Format the datetime as a date |
||
50 | * |
||
51 | * @param mixed $value |
||
52 | * @param string $format |
||
53 | * @return string |
||
54 | */ |
||
55 | 1 | public function asDateTime($value, $format = 'Y-m-d H:i:s') |
|
59 | |||
60 | private $durationUnits = [ |
||
61 | 'Day' => 86400, |
||
62 | 'Hour' => 3600, |
||
63 | 'Minute' => 60, |
||
64 | 'Second' => 1 |
||
65 | ]; |
||
66 | |||
67 | /** |
||
68 | * Take in a number of seconds and display that as a human readable amount |
||
69 | * of time |
||
70 | * |
||
71 | * From: https://jonlabelle.com/snippets/view/php/convert-seconds-to-human-readable |
||
72 | * |
||
73 | * @param int $seconds |
||
74 | * @return string |
||
75 | * @todo allow units to be overridden (non-english..) |
||
76 | */ |
||
77 | 6 | public function asDurationHuman($seconds, $delimit = ', ') |
|
78 | { |
||
79 | 6 | if ($seconds === null) { |
|
80 | 1 | return $this->nullValue; |
|
81 | } |
||
82 | |||
83 | 5 | $parts = []; |
|
84 | |||
85 | 5 | foreach ($this->durationUnits as $label => $duration) { |
|
86 | 5 | $div = floor($seconds / $duration); |
|
87 | 5 | if ($div == 0) { |
|
88 | 5 | continue; |
|
89 | } |
||
90 | |||
91 | 5 | $part = $div . ' ' . $label; |
|
92 | 5 | if ($div != 1) { |
|
93 | 4 | $part .= 's'; |
|
94 | 4 | } |
|
95 | 5 | $parts[] = $part; |
|
96 | |||
97 | 5 | $seconds %= $duration; |
|
98 | 5 | } |
|
99 | |||
100 | 5 | $last = array_pop($parts); |
|
101 | |||
102 | 5 | if (empty($parts)) { |
|
103 | 3 | return $last; |
|
104 | } |
||
105 | |||
106 | 2 | return join($delimit, $parts) . ' and ' . $last; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Generate the output from the value |
||
111 | * |
||
112 | * @param mixed $value the datetime value |
||
113 | * @param string $format format string |
||
114 | * @return string |
||
115 | */ |
||
116 | 5 | private function output($value, $format) |
|
122 | |||
123 | /** |
||
124 | * Convert the datetime input into a instance of Carbon |
||
125 | * |
||
126 | * @param mixed $value the datetime value |
||
127 | * @return Carbon |
||
128 | */ |
||
129 | 5 | private function normaliseValue($value) |
|
138 | |||
139 | /** |
||
140 | * Get an instance of the carbon datetime handler |
||
141 | * |
||
142 | * @return Carbon |
||
143 | * @throws FormatterException if carbon isn't available |
||
144 | */ |
||
145 | 5 | private function getCarbon() |
|
149 | } |
||
150 |