Skip to content

Commit 79f5d3f

Browse files
committed
feat: add DB config dateFormat to provide default date/time formats
1 parent 846466e commit 79f5d3f

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

app/Config/Database.php

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class Database extends Config
4343
'failover' => [],
4444
'port' => 3306,
4545
'numberNative' => false,
46+
'dateFormat' => [
47+
'date' => 'Y-m-d',
48+
'datetime' => 'Y-m-d H:i:s',
49+
'time' => 'H:i:s',
50+
],
4651
];
4752

4853
/**
@@ -69,6 +74,11 @@ class Database extends Config
6974
'port' => 3306,
7075
'foreignKeys' => true,
7176
'busyTimeout' => 1000,
77+
'dateFormat' => [
78+
'date' => 'Y-m-d',
79+
'datetime' => 'Y-m-d H:i:s',
80+
'time' => 'H:i:s',
81+
],
7282
];
7383

7484
public function __construct()

system/Database/BaseConnection.php

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @property float $connectDuration
2727
* @property float $connectTime
2828
* @property string $database
29+
* @property array $dateFormat
2930
* @property string $DBCollat
3031
* @property bool $DBDebug
3132
* @property string $DBDriver
@@ -347,6 +348,19 @@ abstract class BaseConnection implements ConnectionInterface
347348
*/
348349
protected $queryClass = Query::class;
349350

351+
/**
352+
* Default Date/Time formats
353+
*
354+
* @var array<string, string>
355+
*/
356+
protected array $dateFormat = [
357+
'date' => 'Y-m-d',
358+
'datetime' => 'Y-m-d H:i:s',
359+
'datetime-ms' => 'Y-m-d H:i:s.v',
360+
'datetime-us' => 'Y-m-d H:i:s.u',
361+
'time' => 'H:i:s',
362+
];
363+
350364
/**
351365
* Saves our connection settings.
352366
*/
@@ -358,6 +372,10 @@ public function __construct(array $params)
358372
}
359373
}
360374

375+
if (isset($params['dateFormat'])) {
376+
$this->dateFormat = array_merge($this->dateFormat, $params['dateFormat']);
377+
}
378+
361379
$queryClass = str_replace('Connection', 'Query', static::class);
362380

363381
if (class_exists($queryClass)) {

tests/system/Database/BaseConnectionTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function testSavesConfigOptions(): void
8080
$this->assertFalse($db->compress);
8181
$this->assertTrue($db->strictOn);
8282
$this->assertSame([], $db->failover);
83+
$this->assertSame([
84+
'date' => 'Y-m-d',
85+
'datetime' => 'Y-m-d H:i:s',
86+
'datetime-ms' => 'Y-m-d H:i:s.v',
87+
'datetime-us' => 'Y-m-d H:i:s.u',
88+
'time' => 'H:i:s',
89+
], $db->dateFormat);
8390
}
8491

8592
public function testConnectionThrowExceptionWhenCannotConnect(): void

user_guide_src/source/database/configuration.rst

+10
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,18 @@ Explanation of Values:
184184
To enforce Foreign Key constraint, set this config item to true.
185185
**busyTimeout** milliseconds (int) - Sleeps for a specified amount of time when a table is locked (``SQLite3`` only).
186186
**numberNative** true/false (boolean) - Whether or not to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE (``MySQLi`` only).
187+
**dateFormat** The default date/time formats as PHP's `DateTime format`_.
188+
* ``date`` - date format
189+
* ``datetime`` - date and time format
190+
* ``datetime-ms`` - date and time with millisecond format
191+
* ``datetime-us`` - date and time with microsecond format
192+
* ``time`` - time format
193+
This can be used since v4.5.0, and you can get the value, e.g., ``$db->dateFormat[datetime]``.
194+
Currently, the database drivers do not use these values directly, but just provide the values.
187195
================ ===========================================================================================================
188196

197+
.. _DateTime format: https://www.php.net/manual/en/datetime.format.php
198+
189199
.. note:: Depending on what database driver you are using (``MySQLi``, ``Postgre``,
190200
etc.) not all values will be needed. For example, when using ``SQLite3`` you
191201
will not need to supply a username or password, and the database name

0 commit comments

Comments
 (0)