]> _ Git - cubist_matomo.git/commitdiff
wip #5877
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 May 2023 17:00:51 +0000 (19:00 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 24 May 2023 17:00:51 +0000 (19:00 +0200)
src/MatomoUtils.php [new file with mode: 0644]

diff --git a/src/MatomoUtils.php b/src/MatomoUtils.php
new file mode 100644 (file)
index 0000000..03a0ec3
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+namespace Cubist\Matomo;
+
+class MatomoUtils {
+    public static function parseDate($date)
+    {
+        // Match possible date strings:
+        // - YYYY
+        // - YYYY-MM
+        // - YYYY-MM-DD
+        // - YYYY-MM-DD,YYYY-MM-DD
+        // https://regex101.com/r/BLrqm0/1
+        $regex = '/^(?<start_date>(?<start_year>2\d{3})-?(?<start_month>0[1-9]|1[012])?-?(?<start_day>0[1-9]|[12][0-9]|3[01])?),?(?<end_date>2\d{3}-(?>0[1-9]|1[012])-(?>0[1-9]|[12][0-9]|3[01]))?/';
+
+        preg_match($regex, $date, $date_matches);
+
+        extract($date_matches); // Just for easier access to match variables
+
+        // Bail out on nonsensical dates
+        if (isset($start_date) && isset($end_date) && ($start_date > $end_date)) {
+            return false;
+        }
+
+        return $date_matches;
+    }
+}