Core\Time\DateTime::day
$dateTime->day(): uintReturns the day of the month of a date and time, as a uint from 1 to 31.
The day is read in the time zone of the value. At 02:30 UTC on 6 March 2024, day returns 6 in UTC and 5 in New York, where it is still the evening before.
The result has the same name and type as the day option of with. So $d->with({ day: 1 }) gives the first day of the same month, at the same time. with throws a RuntimeError when the day does not exist in that month, such as 31 April.
Good to know: show a date to a user in the user's own zone. Otherwise the day can be one too early or one too late.
Return value
Returns uint. The day, from 1 to the last day of the month.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\DateTime::year, Core\Time\DateTime::month, Core\Time\DateTime::day, Core\Time\DateTime::hour, Core\Time\DateTime::minute, Core\Time\DateTime::second and Core\Time\DateTime::nanoseconds return the parts of a date and time as numbers. Each one reads the clock in the zone of the value. Core\Time\DateTime::year returns an int, and the others return a uint. |
Examples
Read the day
<?nvs// Shows how to read the day of the month of a date and time as a number.
var $delivery = Core\Time\DateTime::fromIso('2024-11-08 11:00', 'Europe/Madrid');
// `day` returns a `uint`. This prints 8.echo $delivery->day(), "\n";
// The readers give the parts of the date. This prints 8.11.2024.echo $delivery->day(), '.', $delivery->month(), '.', $delivery->year(), "\n";88.11.2024The day in two zones
<?nvs// Shows that one moment can be on a different day in two time zones.
// A customer in New York places an order late in the evening. The server saves it in UTC.var $placed = Core\Time\Instant::fromIso('2024-03-06T02:30:00Z');
// In New York it is still the evening of 5 March. This prints 5.echo $placed->in('America/New_York')->day(), "\n";
// In UTC it is already 6 March. This prints 6.echo $placed->in('UTC')->day(), "\n";56Send invoices on the first
<?nvs// Shows how a daily job checks if today is the day to send the monthly invoices.
var $zone = Core\Time\Zone::of('Europe/Vienna');
// A real program uses `Core\Time::now()` here. Fixed days keep the output the same.var $day = Core\Time\DateTime::fromIso('2024-04-29 06:00', $zone);int $runs = 0;while ($runs < 4) { // The invoices go out when the day of the month is 1. if ($day->day() == 1) { echo $day->format('yyyy-MM-dd'), ": send invoices\n"; } else { echo $day->format('yyyy-MM-dd'), ": nothing to do\n"; } $day = $day->plus(1, Core\Unit::Day); $runs = $runs + 1;}2024-04-29: nothing to do2024-04-30: nothing to do2024-05-01: send invoices2024-05-02: nothing to do