DateFormat::dateFormat()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 15
cp 0
rs 8.9777
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sdibble
5
 * Date: 12/12/2016
6
 * Time: 12:11 PM.
7
 */
8
9
namespace SET\Handlers;
10
11
use Carbon\Carbon;
12
13
trait DateFormat
14
{
15
    public function dateFormat($date)
16
    {
17
        if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)) {
18
            return $date;
19
        }
20
21
        if ($date instanceof Carbon || $date instanceof \DateTime) {
22
            return $date->format('Y-m-d');
23
        }
24
25
        // sqlite
26
        if ($carbonDate = \DateTime::createFromFormat('Y-m-d H:i:s', $date)) {
27
            return $carbonDate->format('Y-m-d');
28
        }
29
30
        // JPAS
31
        if ($carbonDate = \DateTime::createFromFormat('n/j/Y G:i', $date)) {
32
            return $carbonDate->format('Y-m-d');
33
        }
34
    }
35
}
36