APIv4 - Add DATEDIFF to calculate days between two dates
authorMatthew Wire <mjw@mjwconsult.co.uk>
Tue, 1 Nov 2022 14:04:02 +0000 (14:04 +0000)
committerColeman Watts <coleman@civicrm.org>
Tue, 1 Nov 2022 19:09:15 +0000 (15:09 -0400)
Co-authored-by: colemanw <coleman@civicrm.org>
Civi/Api4/Query/SqlFunctionDATEDIFF.php [new file with mode: 0644]

diff --git a/Civi/Api4/Query/SqlFunctionDATEDIFF.php b/Civi/Api4/Query/SqlFunctionDATEDIFF.php
new file mode 100644 (file)
index 0000000..cc5a686
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionDATEDIFF extends SqlFunction {
+
+  protected static $category = self::CATEGORY_DATE;
+
+  protected static $dataType = 'Integer';
+
+  protected static function params(): array {
+    return [
+      [
+        'max_expr' => 2,
+        'min_expr' => 2,
+        'optional' => FALSE,
+        'label' => ts('diff'),
+      ],
+    ];
+  }
+
+  /**
+   * @return string
+   */
+  public static function getTitle(): string {
+    return ts('Days between two dates');
+  }
+
+  /**
+   * @return string
+   */
+  public static function getDescription(): string {
+    return ts('Number of days between two dates.');
+  }
+
+}