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