Skip to content

Commit

Permalink
Merge pull request #8538 from kenjis/feat-model-uses-db-dateFormat
Browse files Browse the repository at this point in the history
feat: use $db->dateFormat in Model
  • Loading branch information
kenjis authored Feb 17, 2024
2 parents 4b147f8 + d38f1f2 commit 17381a0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
8 changes: 4 additions & 4 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1356,10 +1356,10 @@ protected function intToDate(int $value)
return $value;

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

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

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

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

case 'int':
return $value->getTimestamp();
Expand Down
6 changes: 6 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ not changed.

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

Saving Dates
------------

Now you can configure the date/time format when you save :doc:`Time <../libraries/time>`
instances. See :ref:`model-saving-dates` for details.

Libraries
=========

Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/database/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ Explanation of Values:
* ``datetime-us`` - date and time with microsecond format
* ``time`` - time format
This can be used since v4.5.0, and you can get the value, e.g., ``$db->dateFormat['datetime']``.
Currently, the database drivers do not use these values directly, but just provide the values.
Currently, the database drivers do not use these values directly,
but :ref:`Model <model-saving-dates>` uses them.
================ ===========================================================================================================

.. _DateTime format: https://www.php.net/manual/en/datetime.format.php
Expand Down
15 changes: 15 additions & 0 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,21 @@ model's ``save()`` method to inspect the class, grab any public and private prop
.. note:: If you find yourself working with Entities a lot, CodeIgniter provides a built-in :doc:`Entity class </models/entities>`
that provides several handy features that make developing Entities simpler.

.. _model-saving-dates:

Saving Dates
------------

.. versionadded:: 4.5.0

When saving data, if you pass :doc:`Time <../libraries/time>` instances, they are
converted to strings with the format defined in ``dateFormat['datetime']`` and
``dateFormat['date']`` in the
:ref:`database configuration <database-config-explanation-of-values>`.

.. note:: Prior to v4.5.0, the date/time formats were hard coded as ``Y-m-d H:i:s``
and ``Y-m-d`` in the Model class.

Deleting Data
=============

Expand Down

0 comments on commit 17381a0

Please sign in to comment.