Skip to content

Core\Time\Date::day

$date->day(): uint

Returns the day of the month of a date, as a uint from 1 to 31.

2024-03-15 returns 15. The first day of every month is 1. The last day is 28, 29, 30 or 31, depending on the month and the year.

The result has the same name and type as the day option of with. So $date->with({ day: 1 }) gives the first day of the same month.

Good to know: a date is the last day of its month when the next day has day equal to 1.

Return value

Returns uint. The day, from 1 to the last day of the month.

Changelog

VersionChange
0.0.5Core\Time\Date::year, Core\Time\Date::month and Core\Time\Date::day return the parts of a date as numbers. Core\Time\Date::weekday returns a Core\Time\Weekday case, and Core\Time\Date::dayOfYear returns the day of the year from 1. Core\Time\Date::isLeapYear checks if the year is a leap year.

Examples

Read the day

<?nvs
// Shows how to read the day of the month of a date as a number.
var $due = Core\Time\Date::at(2024, 3, 15);
// `day` returns a `uint`. This prints 15.
echo $due->day(), "\n";
// The three readers give the parts of the date. This prints 15.3.2024.
echo $due->day(), '.', $due->month(), '.', $due->year(), "\n";
Output
15
15.3.2024

Days until payday

<?nvs
// Shows how to count the days until the next payday on the 25th of the month.
class Payroll
{
public static function daysUntilPayday(Core\Time\Date $today): int
{
var $payday = $today->with({ day: 25 });
// After the 25th, the next payday is in the next month.
if ($today->day() > 25) {
$payday = $payday->plus(1, Core\Unit::Month);
}
var $day = $today;
int $days = 0;
while ($day < $payday) {
$day = $day->plus(1, Core\Unit::Day);
$days = $days + 1;
}
return $days;
}
}
// This prints 10, then 0 on payday, then 29.
echo Payroll::daysUntilPayday(Core\Time\Date::at(2024, 3, 15)), "\n";
echo Payroll::daysUntilPayday(Core\Time\Date::at(2024, 3, 25)), "\n";
echo Payroll::daysUntilPayday(Core\Time\Date::at(2024, 3, 27)), "\n";
Output
10
0
29

The last day of the month

<?nvs
// Shows how to check whether a date is the last day of its month.
// The next day is the 1st only when this date is the last day of the month.
class Calendar
{
public static function isLastDay(Core\Time\Date $date): bool
{
return $date->plus(1, Core\Unit::Day)->day() == 1;
}
}
// February has 29 days in 2024 and 28 days in 2023. This prints no, yes, yes and no.
var $dates = [
Core\Time\Date::at(2024, 2, 28),
Core\Time\Date::at(2024, 2, 29),
Core\Time\Date::at(2023, 2, 28),
Core\Time\Date::at(2024, 12, 30),
];
foreach ($dates as Core\Time\Date $date) {
echo $date->format('yyyy-MM-dd'), ': ', Calendar::isLastDay($date) ? 'yes' : 'no', "\n";
}
Output
2024-02-28: no
2024-02-29: yes
2023-02-28: yes
2024-12-30: no