1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core;
4:
5: use Salient\Contract\Core\DateParserInterface;
6: use Salient\Utility\Test;
7: use DateTimeImmutable;
8: use DateTimeZone;
9:
10: /**
11: * Parses date and time strings understood by strtotime()
12: *
13: * @api
14: */
15: final class DateParser implements DateParserInterface
16: {
17: /**
18: * @inheritDoc
19: */
20: public function parse(string $value, ?DateTimeZone $timezone = null): ?DateTimeImmutable
21: {
22: if (!Test::isDateString($value)) {
23: return null;
24: }
25: $date = new DateTimeImmutable($value, $timezone);
26: if ($timezone) {
27: return $date->setTimezone($timezone);
28: }
29: return $date;
30: }
31: }
32: