Skip to content

Commit ed29b50

Browse files
committed
docs: add notes for datetime ms/us in model and query builder
1 parent 9ad1748 commit ed29b50

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

user_guide_src/source/models/model.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ The datetime format is set in the ``dateFormat`` array of the
395395
:ref:`database configuration <database-config-explanation-of-values>` in the
396396
**app/Config/Database.php** file.
397397

398+
.. note::
399+
When you set ``ms`` or ``us`` as a parameter, **Model** takes care of second's
400+
fractional part of the Time. But **Query Builder** does not. So you still need
401+
to use the ``format()`` method when you pass the Time to Query Builder's methods
402+
like ``where()``:
403+
404+
.. literalinclude:: model/063.php
405+
:lines: 2-
406+
407+
.. note:: Prior to v4.6.0, you cannot use ``ms`` or ``us`` as a parameter.
408+
Because the second's fractional part of Time was lost due to bugs.
409+
398410
Custom Casting
399411
==============
400412

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$model = model('SomeModel');
4+
5+
$now = \CodeIgniter\I18n\Time::now();
6+
7+
// The following code passes the microseconds to Query Builder.
8+
$model->where('my_dt_field', $now->format('Y-m-d H:i:s.u'))->findAll();
9+
// Generates: SELECT * FROM `my_table` WHERE `my_dt_field` = '2024-07-28 18:57:58.900326'
10+
11+
// But the following code loses the microseconds.
12+
$model->where('my_dt_field', $now)->findAll();
13+
// Generates: SELECT * FROM `my_table` WHERE `my_dt_field` = '2024-07-28 18:57:58'

0 commit comments

Comments
 (0)