Conditions | 4 |
Paths | 6 |
Total Lines | 32 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 22 |
CRAP Score | 4 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
48 | 3 | public function getDatetimeFromFormat($format) |
|
49 | { |
||
50 | $formats = [ |
||
51 | 3 | 'Y' => 31104000, |
|
52 | 3 | 'M' => 2592000, |
|
53 | 3 | 'D' => 86400, |
|
54 | 3 | 'H' => 3600, |
|
55 | 3 | 'm' => 60, |
|
56 | 3 | 's' => 1, |
|
57 | 3 | ]; |
|
58 | |||
59 | 3 | $operator = $format{0}; |
|
60 | 3 | $format = substr($format, 1); |
|
61 | 3 | $time = 0; |
|
62 | |||
63 | 3 | foreach ($formats as $scale => $seconds) { |
|
64 | 3 | $data = explode($scale, $format); |
|
65 | |||
66 | 3 | if (strlen($format) === strlen($data[0])) { |
|
67 | 3 | continue; |
|
68 | } |
||
69 | 3 | $time += $data[0] * $seconds; |
|
70 | // Remaining format string |
||
71 | 3 | $format = $data[1]; |
|
72 | 3 | } |
|
73 | |||
74 | return |
||
75 | 3 | ($operator === '+') |
|
76 | 3 | ? (new \DateTime())->setTimestamp((time() + $time)) |
|
77 | 3 | : (new \DateTime())->setTimestamp((time() - $time)) |
|
78 | 3 | ; |
|
79 | } |
||
80 | } |
||
81 |