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