Skip to content

Commit 17381a0

Browse files
authored
Merge pull request #8538 from kenjis/feat-model-uses-db-dateFormat
feat: use $db->dateFormat in Model
2 parents 4b147f8 + d38f1f2 commit 17381a0

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

system/BaseModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,10 +1356,10 @@ protected function intToDate(int $value)
13561356
return $value;
13571357

13581358
case 'datetime':
1359-
return date('Y-m-d H:i:s', $value);
1359+
return date($this->db->dateFormat['datetime'], $value);
13601360

13611361
case 'date':
1362-
return date('Y-m-d', $value);
1362+
return date($this->db->dateFormat['date'], $value);
13631363

13641364
default:
13651365
throw ModelException::forNoDateFormat(static::class);
@@ -1382,10 +1382,10 @@ protected function timeToDate(Time $value)
13821382
{
13831383
switch ($this->dateFormat) {
13841384
case 'datetime':
1385-
return $value->format('Y-m-d H:i:s');
1385+
return $value->format($this->db->dateFormat['datetime']);
13861386

13871387
case 'date':
1388-
return $value->format('Y-m-d');
1388+
return $value->format($this->db->dateFormat['date']);
13891389

13901390
case 'int':
13911391
return $value->getTimestamp();

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ not changed.
317317

318318
See :ref:`Using CodeIgniter’s Model <model-update-only-changed>` for details.
319319

320+
Saving Dates
321+
------------
322+
323+
Now you can configure the date/time format when you save :doc:`Time <../libraries/time>`
324+
instances. See :ref:`model-saving-dates` for details.
325+
320326
Libraries
321327
=========
322328

user_guide_src/source/database/configuration.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ Explanation of Values:
191191
* ``datetime-us`` - date and time with microsecond format
192192
* ``time`` - time format
193193
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.
194+
Currently, the database drivers do not use these values directly,
195+
but :ref:`Model <model-saving-dates>` uses them.
195196
================ ===========================================================================================================
196197

197198
.. _DateTime format: https://www.php.net/manual/en/datetime.format.php

user_guide_src/source/models/model.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,21 @@ model's ``save()`` method to inspect the class, grab any public and private prop
458458
.. note:: If you find yourself working with Entities a lot, CodeIgniter provides a built-in :doc:`Entity class </models/entities>`
459459
that provides several handy features that make developing Entities simpler.
460460

461+
.. _model-saving-dates:
462+
463+
Saving Dates
464+
------------
465+
466+
.. versionadded:: 4.5.0
467+
468+
When saving data, if you pass :doc:`Time <../libraries/time>` instances, they are
469+
converted to strings with the format defined in ``dateFormat['datetime']`` and
470+
``dateFormat['date']`` in the
471+
:ref:`database configuration <database-config-explanation-of-values>`.
472+
473+
.. note:: Prior to v4.5.0, the date/time formats were hard coded as ``Y-m-d H:i:s``
474+
and ``Y-m-d`` in the Model class.
475+
461476
Deleting Data
462477
=============
463478

0 commit comments

Comments
 (0)