Skip to content

Commit 596e443

Browse files
authored
Merge pull request #6431 from kenjis/feat-Time-toDatabase
feat: add Time::toDatabase()
2 parents c9002e5 + 64b6684 commit 596e443

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

system/I18n/Time.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ public function toTimeString()
895895
*
896896
* @throws Exception
897897
*
898-
* @return bool|string
898+
* @return false|string
899899
*/
900900
public function toLocalizedString(?string $format = null)
901901
{
@@ -1179,4 +1179,12 @@ public function __wakeup(): void
11791179
$this->timezone = new DateTimeZone($timezone);
11801180
parent::__construct($this->date, $this->timezone);
11811181
}
1182+
1183+
/**
1184+
* Returns the datetime string ('Y-m-d H:i:s') that is safe to databases regardless of locale.
1185+
*/
1186+
public function toDatabase(): string
1187+
{
1188+
return $this->format('Y-m-d H:i:s');
1189+
}
11821190
}

tests/system/I18n/TimeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,4 +1128,17 @@ public function testSetTestNowWithFaLocale()
11281128

11291129
Locale::setDefault($currentLocale);
11301130
}
1131+
1132+
public function testToDatabase()
1133+
{
1134+
$currentLocale = Locale::getDefault();
1135+
Locale::setDefault('fa');
1136+
1137+
$time = Time::parse('2017-01-12 00:00', 'America/Chicago');
1138+
1139+
$this->assertSame('۲۰۱۷-۰۱-۱۲ ۰۰:۰۰:۰۰', (string) $time);
1140+
$this->assertSame('2017-01-12 00:00:00', $time->toDatabase());
1141+
1142+
Locale::setDefault($currentLocale);
1143+
}
11311144
}

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Others
7070
- Added new Form helper function :php:func:`validation_errors()`, :php:func:`validation_list_errors()` and :php:func:`validation_show_error()` to display Validation Errors.
7171
- Now you can autoload helpers by **app/Config/Autoload.php**.
7272
- ``BaseConnection::escape()`` now excludes the ``RawSql`` data type. This allows passing SQL strings into data.
73+
- Added :ref:`Time::toDatabase() <time-todatabase>` to get a datetime string that can be used with databases regardless of locale.
7374

7475
Changes
7576
*******

user_guide_src/source/libraries/time.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ Displays just the localized version of time portion of the value:
171171

172172
.. literalinclude:: time/018.php
173173

174+
.. _time-todatabase:
175+
176+
toDatabase()
177+
============
178+
179+
.. versionadded:: 4.3.0
180+
181+
This method returns a string that can be used with databases regardless of locale.
182+
183+
.. literalinclude:: time/042.php
184+
174185
humanize()
175186
==========
176187

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
// Locale: en
4+
$time = Time::parse('March 9, 2016 12:00:00', 'America/Chicago');
5+
echo $time->toDatabase(); // '2016-03-09 12:00:00'
6+
7+
// Locale: fa
8+
$time = Time::parse('March 9, 2016 12:00:00', 'America/Chicago');
9+
echo $time->toDatabase(); // '2016-03-09 12:00:00'

0 commit comments

Comments
 (0)