Skip to content

Commit 9cb035a

Browse files
authored
Merge pull request #441 from cakephp/endofday-micro
Add microseconds to Chronos::endOfDay()
2 parents 4815c4e + 3d06bd9 commit 9cb035a

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/Chronos.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ public function subSeconds(int $value): static
14251425
}
14261426

14271427
/**
1428-
* Resets the time to 00:00:00
1428+
* Sets the time to 00:00:00
14291429
*
14301430
* @return static
14311431
*/
@@ -1435,17 +1435,23 @@ public function startOfDay(): static
14351435
}
14361436

14371437
/**
1438-
* Resets the time to 23:59:59
1438+
* Sets the time to 23:59:59 or 23:59:59.999999
1439+
* if `$microseconds` is true.
14391440
*
1441+
* @param bool $microseconds Whether to set microseconds
14401442
* @return static
14411443
*/
1442-
public function endOfDay(): static
1444+
public function endOfDay(bool $microseconds = false): static
14431445
{
1446+
if ($microseconds) {
1447+
return $this->modify('23:59:59.999999');
1448+
}
1449+
14441450
return $this->modify('23:59:59');
14451451
}
14461452

14471453
/**
1448-
* Resets the date to the first day of the month and the time to 00:00:00
1454+
* Sets the date to the first day of the month and the time to 00:00:00
14491455
*
14501456
* @return static
14511457
*/
@@ -1455,7 +1461,7 @@ public function startOfMonth(): static
14551461
}
14561462

14571463
/**
1458-
* Resets the date to end of the month and time to 23:59:59
1464+
* Sets the date to end of the month and time to 23:59:59
14591465
*
14601466
* @return static
14611467
*/
@@ -1465,7 +1471,7 @@ public function endOfMonth(): static
14651471
}
14661472

14671473
/**
1468-
* Resets the date to the first day of the year and the time to 00:00:00
1474+
* Sets the date to the first day of the year and the time to 00:00:00
14691475
*
14701476
* @return static
14711477
*/
@@ -1475,7 +1481,7 @@ public function startOfYear(): static
14751481
}
14761482

14771483
/**
1478-
* Resets the date to end of the year and time to 23:59:59
1484+
* Sets the date to end of the year and time to 23:59:59
14791485
*
14801486
* @return static
14811487
*/
@@ -1485,7 +1491,7 @@ public function endOfYear(): static
14851491
}
14861492

14871493
/**
1488-
* Resets the date to the first day of the decade and the time to 00:00:00
1494+
* Sets the date to the first day of the decade and the time to 00:00:00
14891495
*
14901496
* @return static
14911497
*/
@@ -1497,7 +1503,7 @@ public function startOfDecade(): static
14971503
}
14981504

14991505
/**
1500-
* Resets the date to end of the decade and time to 23:59:59
1506+
* Sets the date to end of the decade and time to 23:59:59
15011507
*
15021508
* @return static
15031509
*/
@@ -1509,7 +1515,7 @@ public function endOfDecade(): static
15091515
}
15101516

15111517
/**
1512-
* Resets the date to the first day of the century and the time to 00:00:00
1518+
* Sets the date to the first day of the century and the time to 00:00:00
15131519
*
15141520
* @return static
15151521
*/
@@ -1523,7 +1529,7 @@ public function startOfCentury(): static
15231529
}
15241530

15251531
/**
1526-
* Resets the date to end of the century and time to 23:59:59
1532+
* Sets the date to end of the century and time to 23:59:59
15271533
*
15281534
* @return static
15291535
*/
@@ -1542,7 +1548,7 @@ public function endOfCentury(): static
15421548
}
15431549

15441550
/**
1545-
* Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
1551+
* Sets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00
15461552
*
15471553
* @return static
15481554
*/
@@ -1557,7 +1563,7 @@ public function startOfWeek(): static
15571563
}
15581564

15591565
/**
1560-
* Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
1566+
* Sets the date to end of week (defined in $weekEndsAt) and time to 23:59:59
15611567
*
15621568
* @return static
15631569
*/

tests/TestCase/DateTime/StartEndOfTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public function testEndOfDay()
3333
$now = Chronos::now();
3434
$dt = $now->endOfDay();
3535
$this->assertTrue($dt instanceof Chronos);
36-
$this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59);
36+
$this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59, 0);
37+
38+
$dt = $now->endOfDay(true);
39+
$this->assertTrue($dt instanceof Chronos);
40+
$this->assertDateTime($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59, 999999);
3741
}
3842

3943
public function testStartOfMonthIsFluid()

0 commit comments

Comments
 (0)