ipagdevs /
ipag-sdk-php
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Ipag\Sdk\Util; |
||
| 4 | |||
| 5 | use DateTime; |
||
| 6 | use DateTimeInterface; |
||
| 7 | use InvalidArgumentException; |
||
| 8 | |||
| 9 | abstract class DateUtil |
||
| 10 | { |
||
| 11 | public const ISO_DATE_FORMAT = 'Y-m-d'; |
||
| 12 | |||
| 13 | public static function parseDate($date, string $format = self::ISO_DATE_FORMAT): DateTimeInterface |
||
| 14 | { |
||
| 15 | $date = self::tryParseDate($date, $format); |
||
| 16 | |||
| 17 | if (!$date) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 18 | throw new InvalidArgumentException("Invalid date format ($date does not conform to $format)"); |
||
| 19 | } |
||
| 20 | |||
| 21 | return $date; |
||
| 22 | } |
||
| 23 | |||
| 24 | public static function tryParseDate($date, string $format = self::ISO_DATE_FORMAT): ?DateTimeInterface |
||
| 25 | { |
||
| 26 | if (is_null($date) || $date instanceof DateTimeInterface) { |
||
| 27 | return $date; |
||
| 28 | } |
||
| 29 | |||
| 30 | return DateTime::createFromFormat($format, $date) ?: null; |
||
| 31 | } |
||
| 32 | } |
||
| 33 |