Skip to content

Commit

Permalink
Add microseconds to Chronos::endOfDay()
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Taylor committed Oct 17, 2023
1 parent 4815c4e commit 3d06bd9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
32 changes: 19 additions & 13 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ public function subSeconds(int $value): static
}

/**
* Resets the time to 00:00:00
* Sets the time to 00:00:00
*
* @return static
*/
Expand All @@ -1435,17 +1435,23 @@ public function startOfDay(): static
}

/**
* Resets the time to 23:59:59
* Sets the time to 23:59:59 or 23:59:59.999999
* if `$microseconds` is true.
*
* @param bool $microseconds Whether to set microseconds
* @return static
*/
public function endOfDay(): static
public function endOfDay(bool $microseconds = false): static
{
if ($microseconds) {
return $this->modify('23:59:59.999999');
}

return $this->modify('23:59:59');
}

/**
* Resets the date to the first day of the month and the time to 00:00:00
* Sets the date to the first day of the month and the time to 00:00:00
*
* @return static
*/
Expand All @@ -1455,7 +1461,7 @@ public function startOfMonth(): static
}

/**
* Resets the date to end of the month and time to 23:59:59
* Sets the date to end of the month and time to 23:59:59
*
* @return static
*/
Expand All @@ -1465,7 +1471,7 @@ public function endOfMonth(): static
}

/**
* Resets the date to the first day of the year and the time to 00:00:00
* Sets the date to the first day of the year and the time to 00:00:00
*
* @return static
*/
Expand All @@ -1475,7 +1481,7 @@ public function startOfYear(): static
}

/**
* Resets the date to end of the year and time to 23:59:59
* Sets the date to end of the year and time to 23:59:59
*
* @return static
*/
Expand All @@ -1485,7 +1491,7 @@ public function endOfYear(): static
}

/**
* Resets the date to the first day of the decade and the time to 00:00:00
* Sets the date to the first day of the decade and the time to 00:00:00
*
* @return static
*/
Expand All @@ -1497,7 +1503,7 @@ public function startOfDecade(): static
}

/**
* Resets the date to end of the decade and time to 23:59:59
* Sets the date to end of the decade and time to 23:59:59
*
* @return static
*/
Expand All @@ -1509,7 +1515,7 @@ public function endOfDecade(): static
}

/**
* Resets the date to the first day of the century and the time to 00:00:00
* Sets the date to the first day of the century and the time to 00:00:00
*
* @return static
*/
Expand All @@ -1523,7 +1529,7 @@ public function startOfCentury(): static
}

/**
* Resets the date to end of the century and time to 23:59:59
* Sets the date to end of the century and time to 23:59:59
*
* @return static
*/
Expand All @@ -1542,7 +1548,7 @@ public function endOfCentury(): static
}

/**
* Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
* Sets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
*
* @return static
*/
Expand All @@ -1557,7 +1563,7 @@ public function startOfWeek(): static
}

/**
* Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
* Sets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
*
* @return static
*/
Expand Down
6 changes: 5 additions & 1 deletion tests/TestCase/DateTime/StartEndOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public function testEndOfDay()
$now = Chronos::now();
$dt = $now->endOfDay();
$this->assertTrue($dt instanceof Chronos);
$this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59);
$this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59, 0);

$dt = $now->endOfDay(true);
$this->assertTrue($dt instanceof Chronos);
$this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59, 999999);
}

public function testStartOfMonthIsFluid()
Expand Down

0 comments on commit 3d06bd9

Please sign in to comment.