--- /dev/null
+<?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 SqlFunctionQUARTER extends SqlFunction {
+
+ protected static $category = self::CATEGORY_DATE;
+
+ protected static $dataType = 'Integer';
+
+ protected static function params(): array {
+ return [
+ [
+ 'max_expr' => 1,
+ 'optional' => FALSE,
+ ],
+ ];
+ }
+
+ /**
+ * @return string
+ */
+ public static function getTitle(): string {
+ return ts('Quarter only');
+ }
+
+ /**
+ * @return string
+ */
+ public static function getDescription(): string {
+ return ts('The numeric quarter (1-4) of a date.');
+ }
+
+}
$result = Contact::get(FALSE)
->addSelect('DATEDIFF("2010-01-01", birth_date) AS diff')
->addSelect('YEAR(birth_date) AS year')
+ ->addSelect('QUARTER(birth_date) AS quarter')
->addSelect('MONTH(birth_date) AS month')
->addSelect('EXTRACT(YEAR_MONTH FROM birth_date) AS year_month')
->addWhere('last_name', '=', $lastName)
$this->assertEquals(51, $result[0]['diff']);
$this->assertEquals(2009, $result[0]['year']);
+ $this->assertEquals(4, $result[0]['quarter']);
$this->assertEquals(11, $result[0]['month']);
$this->assertEquals('200911', $result[0]['year_month']);
$this->assertEquals(0, $result[1]['diff']);
$this->assertEquals(2010, $result[1]['year']);
+ $this->assertEquals(1, $result[1]['quarter']);
$this->assertEquals(1, $result[1]['month']);
$this->assertEquals('201001', $result[1]['year_month']);
}