Completed
Pull Request — master (#52)
by Shawn
09:41 queued 06:50
created

DateFormat::dateFormat()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
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
        // Already Y-m-d
18
        if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)) {
19
            return $date;
20
        }
21
22
        //Convert Carbon to Y-m-d
23
        if ($date instanceof Carbon) {
24
            return $date->format('Y-m-d');
25
        }
26
27
        // sqlite Y-m-d H:i:s to Y-m-d
28
        return Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
29
    }
30
}
31