Skip to content

feat: use $db->dateFormat in Model #8538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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