Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public function set(mixed $value): mixed
}

if ($value instanceof DateTimeInterface) {
return $value->format($this->format);
$formatted = $value->format($this->format);

if ($this->format === 'Y-m-d H:i:s' && $value->format('u') !== '000000') {
return $formatted . '.' . $value->format('u');
}

return $formatted;
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ public function whereDateBetween(string $column, $start, $end): self
*/
public function whereDateTimeBetween(string $column, $start, $end): self
{
$start = $this->formatDateTime($start, true);
$end = $this->formatDateTime($end, true);
$start = $this->formatDateTime($start, 'start');
$end = $this->formatDateTime($end, 'end');

return $this->whereBetween($column, [$start, $end]);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Builder/TimeframeOrConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ public function testFormatDateTimeWithDateTimeInterface()
$this->assertEquals('2024-03-20 15:45:00', $b->formatDateTime($dt));
}

public function testWhereDateTimeBetweenUsesStartAndEndOfDayForDateStrings(): void
{
$builder = $this->builder();

$builder->whereDateTimeBetween('created_at', '2024-03-20', '2024-03-21');

$property = (new \ReflectionClass(Builder::class))->getProperty('conditions');

$this->assertSame([
[
'AND',
'created_at',
'BETWEEN',
'2024-03-20 00:00:00',
'2024-03-21 23:59:59',
'AND',
],
], $property->getValue($builder));
}

public function testFormatDateTimePreservesMicroseconds(): void
{
$b = $this->builder();
Expand Down
8 changes: 8 additions & 0 deletions tests/Model/CastSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ public function testCustomDateTimeFormatCast(): void
$this->assertSame('2024-06-15', $model->invoiced_at->format('Y-m-d'));
}

public function testDateTimeCastSetPreservesNonZeroMicroseconds(): void
{
$handler = CastManager::resolve('datetime');
$value = Carbon::createFromFormat('Y-m-d H:i:s.u', '2024-06-15 09:30:00.123456');

$this->assertSame('2024-06-15 09:30:00.123456', $handler->set($value));
}

public function testDateTimeCastNullPassthrough(): void
{
$model = new MockDateCastModel(['published_at' => null]);
Expand Down
Loading