Skip to content

Commit 96a3341

Browse files
committed
feat: add Time::toDatabase()
1 parent c9002e5 commit 96a3341

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

system/I18n/Time.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

0 commit comments

Comments
 (0)