Completed
Push — master ( bba925...6f9e63 )
by Radu
07:51
created

DateHelper::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Helpers;
6
7
class DateHelper
8
{
9
    /**
10
    * Format a date, with validation.
11
    */
12
    public static function format(string $date, string $format = 'Y-m-d'): string
13
    {
14
        $dateTime = \DateTime::createFromFormat($format, $date);
15
        if (false === $dateTime) {
16
            throw new \WebServCo\Framework\Exceptions\DateTimeException('Invalid date or format.');
17
        }
18
        return $dateTime->format($format);
19
    }
20
}
21