Core\Time\DateTime::endOf
$dateTime->endOf(Core\Unit $u): Core\Time\DateTime$dateTime->endOf( Core\Unit $u, ): Core\Time\DateTime
Returns the last moment of the hour, day, week, month or year that a date and time is in.
You give a unit from Core\Unit. With Core\Unit::Day the result is 23:59:59.999999999 on the same day. With Core\Unit::Month it is that time on the last day of the month. This is one nanosecond before the next day or month starts. A week ends on Sunday. The result is in the same time zone, and the value you call endOf on does not change.
Use it for a limit that includes the whole period, such as a sale that runs until the end of the month.
Good to know: the last moment is not a full unit after the first one. On a day with 25 hours, difference counts 24 whole hours from the startOf result to the endOf result.
Parameters
$uCore\UnitThe unit whose last instant to find.
Return value
Returns Core\Time\DateTime. A new DateTime in the same zone, at or after the receiver.
Errors
RuntimeErrorThe result lies outside the representable range.
Examples
End of day month and year
<?nvs// Shows how to find the last moment of the day, the month and the year that a date and time is in.
var $at = Core\Time\DateTime::fromIso('2024-02-10 08:05', 'Asia/Singapore');string $pattern = 'yyyy-MM-dd HH:mm:ss.SSSSSSSSS';
// `endOf` returns the last nanosecond of the unit. That is one nanosecond before the next one starts.// This prints 2024-02-10 23:59:59.999999999.echo $at->endOf(Core\Unit::Day)->format($pattern), "\n";
// 2024 is a leap year, so February has 29 days. This prints 2024-02-29 23:59:59.999999999.echo $at->endOf(Core\Unit::Month)->format($pattern), "\n";
// This prints 2024-12-31 23:59:59.999999999.echo $at->endOf(Core\Unit::Year)->format($pattern), "\n";2024-02-10 23:59:59.9999999992024-02-29 23:59:59.9999999992024-12-31 23:59:59.999999999A day with 25 hours
<?nvs// Shows `startOf` and `endOf` on a day when the clocks change.
// On 27 October 2024, Berlin moves its clocks from 03:00 back to 02:00, so that day has 25 hours.var $sunday = Core\Time\DateTime::fromIso('2024-10-27 12:00', 'Europe/Berlin');var $start = $sunday->startOf(Core\Unit::Day);var $end = $sunday->endOf(Core\Unit::Day);
// The day starts in summer time and ends in winter time.// This prints 00:00 +02:00 and 23:59 +01:00.echo $start->format('HH:mm XXX'), ' and ', $end->format('HH:mm XXX'), "\n";
// `difference` counts whole hours. The end is one nanosecond before the next day starts.// This prints 24.echo $start->difference($end, Core\Unit::Hour), "\n";00:00 +02:00 and 23:59 +01:0024How long a sale runs
<?nvs// Shows how a shop tells its customers how long a sale still runs. The sale ends with the month.
var $now = Core\Time\DateTime::fromIso('2024-04-28 18:30', 'America/Chicago');
// `endOf(Core\Unit::Month)` gives the last moment of April. This prints 30 April at 23:59.var $saleEnd = $now->endOf(Core\Unit::Month);echo 'The sale ends on ', $saleEnd->format("d MMMM 'at' HH:mm"), "\n";
// `difference` counts whole days and whole hours until the sale ends.// This prints 2 and 53.echo 'Days left: ', $now->difference($saleEnd, Core\Unit::Day), "\n";echo 'Hours left: ', $now->difference($saleEnd, Core\Unit::Hour), "\n";The sale ends on 30 April at 23:59Days left: 2Hours left: 53