Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DateTimeTrait
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 detectDateTimeFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 detectFormat
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Utils;
15
16/**
17 * Pop utils date-time format helper trait
18 *
19 * @category   Pop
20 * @package    Pop\Utils
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    2.1.0
25 */
26trait DateTimeTrait
27{
28
29    /**
30     * Date-time format
31     * @var ?string
32     */
33    protected ?string $dateTimeFormat = null;
34
35    /**
36     * Date-time formats
37     * @var array
38     */
39    protected array $dateTimeFormats = [
40        '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}$/' => 'Y-m-d H:i:s.v',
41        '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/'        => 'Y-m-d H:i:s',
42        '/^\d{2}\/\d{2}\/\d{4} \d{1,2}:\d{2}\s(A|P)M$/'  => 'm/d/Y g:i A',
43        '/^\d{2}\/\d{2}\/\d{2} \d{1,2}:\d{2}\s(A|P)M$/'  => 'm/d/y g:i A',
44        '/^\d{4}-\d{2}-\d{2}$/'                          => 'Y-m-d',
45        '/^\d{2}\/\d{2}\/\d{4}$/'                        => 'm/d/Y',
46        '/^\d{2}\.\d{2}\.\d{4}$/'                        => 'd.m.Y',
47        '/^\d{2}\/\d{2}\/\d{2}$/'                        => 'm/d/y',
48        '/^\d{2}\.\d{2}\.\d{2}$/'                        => 'd.m.y',
49        '/^\d{2}:\d{2}:\d{2}$/'                          => 'H:i:s',
50        '/^\d{1,2}:\d{2}\s(A|P)M$/'                      => 'g:i A',
51    ];
52
53    /**
54     * Detect date-time format
55     *
56     * @param  string $dateTime
57     * @return ?string
58     */
59    public static function detectDateTimeFormat(string $dateTime): ?string
60    {
61        return (new self())->detectFormat($dateTime);
62    }
63
64    /**
65     * Detect date-time format
66     *
67     * @param  string $dateTime
68     * @return ?string
69     */
70    public function detectFormat(string $dateTime): ?string
71    {
72        foreach ($this->dateTimeFormats as $regex => $format) {
73            if (preg_match($regex, $dateTime)) {
74                $this->dateTimeFormat = $format;
75                break;
76            }
77        }
78
79        return $this->dateTimeFormat;
80    }
81}