• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / datagrid / 8230471191

11 Mar 2024 09:25AM UTC coverage: 34.102%. First build
8230471191

Pull #1060

github

radimvaculik
Fix phpstan
Pull Request #1060: [7.x] Next

117 of 435 new or added lines in 54 files covered. (26.9%)

1124 of 3296 relevant lines covered (34.1%)

0.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Utils/DateTimeHelper.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\Utils;
4

5
use Contributte\Datagrid\Exception\DatagridDateTimeHelperException;
6
use DateTime;
7
use DateTimeImmutable;
8
use DateTimeZone;
9

10
final class DateTimeHelper
11
{
12

13
        /**
14
         * Try to convert string into \DateTime object
15
         *
16
         * @param array|string[] $formats
17
         * @throws DatagridDateTimeHelperException
18
         */
19
        public static function tryConvertToDateTime(mixed $value, array $formats = []): DateTime
20
        {
21
                return self::fromString($value, $formats);
×
22
        }
23

24
        /**
25
         * Try to convert string into \DateTime object from more date formats
26
         *
27
         * @param array|string[] $formats
28
         * @throws DatagridDateTimeHelperException
29
         */
30
        public static function tryConvertToDate(mixed $value, array $formats = []): DateTime
31
        {
32
                return self::fromString($value, $formats);
×
33
        }
34

35
        /**
36
         * Convert string into \DateTime object from more date without time
37
         *
38
         * @param array|string[] $formats
39
         * @throws DatagridDateTimeHelperException
40
         */
41
        public static function fromString(mixed $value, array $formats = []): DateTime
42
        {
43
                $formats = array_merge($formats, [
×
44
                        'Y-m-d H:i:s.u',
×
45
                        'Y-m-d H:i:s',
46
                        'Y-m-d',
47
                        'j. n. Y G:i:s',
48
                        'j. n. Y G:i',
49
                        'j. n. Y',
50
                        'U',
51
                ]);
52

NEW
53
                if ($value instanceof DateTime) {
×
54
                        return $value;
×
55
                }
56

57
                if ($value instanceof DateTimeImmutable) {
×
58
                        /** @var DateTimeZone|false $tz */
59
                        $tz = $value->getTimezone();
×
NEW
60
                        $date = new DateTime('now', $tz !== false ? $tz : null);
×
61
                        $date->setTimestamp($value->getTimestamp());
×
62

63
                        return $date;
×
64
                }
65

66
                foreach ($formats as $format) {
×
NEW
67
                        $date = DateTime::createFromFormat($format, (string) $value);
×
68

69
                        if ($date === false) {
×
70
                                continue;
×
71
                        }
72

73
                        return $date;
×
74
                }
75

76
                $timestamp = strtotime((string) $value);
×
77

78
                if ($timestamp !== false) {
×
NEW
79
                        $date = new DateTime();
×
80
                        $date->setTimestamp($timestamp);
×
81

82
                        return $date;
×
83
                }
84

NEW
85
                throw new DatagridDateTimeHelperException();
×
86
        }
87

88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc