Core\Time\DateTime::startOf
$dateTime->startOf(Core\Unit $u): Core\Time\DateTime$dateTime->startOf( Core\Unit $u, ): Core\Time\DateTime
Returns the first 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 the start of that day. With Core\Unit::Month it is the start of the first day of the month. A week starts on Monday. A quarter is three months, and the quarters start in January, April, July and October. The result is in the same time zone, and the value you call startOf on does not change.
Use it to find the period that a value belongs to, for example the month that a report covers.
Good to know: a day usually starts at 00:00, but not always. In some time zones the clocks move forward at midnight. On that day the first moment is 01:00.
Parameters
$uCore\UnitThe unit to truncate to;Unit::Weekstarts on Monday andUnit::Quarteron its first month.
Return value
Returns Core\Time\DateTime. A new DateTime in the same zone, at or before the receiver.
Errors
RuntimeErrorThe result lies outside the representable range.
Examples
Start of day month and year
<?nvs// Shows how to find the start of the hour, the day, the month and the year that a date and time is in.
var $at = Core\Time\DateTime::fromIso('2024-08-21 17:42:09', 'Europe/Madrid');
// `startOf` returns the first moment of the unit you give it.// This prints 2024-08-21 17:00:00.echo $at->startOf(Core\Unit::Hour)->format('yyyy-MM-dd HH:mm:ss'), "\n";
// This prints 2024-08-21 00:00:00.echo $at->startOf(Core\Unit::Day)->format('yyyy-MM-dd HH:mm:ss'), "\n";
// This prints 2024-08-01 00:00:00.echo $at->startOf(Core\Unit::Month)->format('yyyy-MM-dd HH:mm:ss'), "\n";
// This prints 2024-01-01 00:00:00.echo $at->startOf(Core\Unit::Year)->format('yyyy-MM-dd HH:mm:ss'), "\n";2024-08-21 17:00:002024-08-21 00:00:002024-08-01 00:00:002024-01-01 00:00:00Weeks quarters and a day without midnight
<?nvs// Shows where a week and a quarter start, and a day that does not start at midnight.
var $friday = Core\Time\DateTime::fromIso('2024-08-23 10:00', 'Europe/Madrid');
// A week starts on Monday. This prints Monday 2024-08-19.echo $friday->startOf(Core\Unit::Week)->format('EEEE yyyy-MM-dd'), "\n";
// A quarter is three months, and the third quarter starts on 1 July. This prints 2024-07-01.echo $friday->startOf(Core\Unit::Quarter)->format('yyyy-MM-dd'), "\n";
// On 4 November 2018, São Paulo moved its clocks from 00:00 to 01:00.// That day had no midnight, so it started at 01:00. This prints 2018-11-04 01:00.var $saoPaulo = Core\Time\DateTime::fromIso('2018-11-04 12:00', 'America/Sao_Paulo');echo $saoPaulo->startOf(Core\Unit::Day)->format('yyyy-MM-dd HH:mm'), "\n";Monday 2024-08-192024-07-012018-11-04 01:00The period of a monthly report
<?nvs// Shows how a monthly report finds the period it covers, which is all of last month.
var $shop = Core\Time\Zone::of('Europe/Madrid');var $today = Core\Time\DateTime::fromIso('2024-03-12 07:15', $shop);
// The report starts at the first moment of last month. It stops when this month starts.// This prints From 2024-02-01 00:00 to 2024-03-01 00:00.var $to = $today->startOf(Core\Unit::Month);var $from = $to->minus(1, Core\Unit::Month);echo 'From ', $from->format('yyyy-MM-dd HH:mm'), ' to ', $to->format('yyyy-MM-dd HH:mm'), "\n";
// Both orders are on 29 February in UTC. In Madrid, the second one is already on 1 March.array<string> $orders = ['2024-02-29T22:30:00Z', '2024-02-29T23:30:00Z'];
// An order is in the report when it is at or after `$from`, and before `$to`.// This prints yes and then no.foreach ($orders as string $placed) { var $at = Core\Time\Instant::fromIso($placed)->in($shop); if ($from->difference($at, Core\Unit::Second) >= 0 && $at->difference($to, Core\Unit::Second) > 0) { echo $placed, " is in the report: yes\n"; } else { echo $placed, " is in the report: no\n"; }}From 2024-02-01 00:00 to 2024-03-01 00:002024-02-29T22:30:00Z is in the report: yes2024-02-29T23:30:00Z is in the report: no