Core\Time\DateTime::month
$dateTime->month(): uintReturns the month of a date and time, as a uint from 1 to 12.
January is 1 and December is 12. The month is read in the time zone of the value. At 23:30 UTC on 31 March, month returns 3 in UTC and 4 in Tokyo.
The result has the same name and type as the month option of with. So $d->with({ month: $d->month() + 1 }) gives the same day in the next month. The month option must be from 1 to 12. In December, $d->month() + 1 is 13, and with throws a RuntimeError. with also throws when the day does not exist in the new month, such as 31 April.
Good to know: plus(1, Core\Unit::Month) also moves to the next month, and it goes from December to January of the next year.
Return value
Returns uint. The month, from 1 for January to 12 for December.
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 month
<?nvs// Shows how to read the month of a date and time as a number.
var $shipped = Core\Time\DateTime::fromIso('2024-09-03 16:00', 'Europe/Paris');
// `month` returns a `uint` from 1 to 12. September is month 9, so this prints 9.echo $shipped->month(), "\n";
// `format` with `MMMM` gives the name of the month. This prints September.echo $shipped->format('MMMM'), "\n";9SeptemberThe season of a month
<?nvs// Shows how to use the month as a position in a list, to pick a season for a shop banner.
// The list has one season for each month, from January to December.array<string> $seasons = [ 'winter', 'winter', 'spring', 'spring', 'spring', 'summer', 'summer', 'summer', 'autumn', 'autumn', 'autumn', 'winter',];var $zone = Core\Time\Zone::of('Europe/Rome');
// The months start at 1 and the list starts at 0, so the position is the month minus 1.// This prints spring, summer and winter.array<string> $days = ['2024-04-10', '2024-07-01', '2024-12-24'];foreach ($days as string $text) { var $day = Core\Time\DateTime::parse($text, 'yyyy-MM-dd', $zone); int $position = ($day->month() - 1) as int; echo $text, ': ', $seasons[$position], "\n";}2024-04-10: spring2024-07-01: summer2024-12-24: winterThe first day of next month
<?nvs// Shows how to find the first day of the next month, when a monthly report is due.
class Report{ public static function dueDate(Core\Time\DateTime $today): Core\Time\DateTime { // In December the next month is January of the next year. if ($today->month() == 12) { return $today->with({ year: $today->year() + 1, month: 1, day: 1 }); } return $today->with({ month: $today->month() + 1, day: 1 }); }}
var $zone = Core\Time\Zone::of('Asia/Singapore');
// This prints 2024-07-01 and then 2025-01-01.echo Report::dueDate(Core\Time\DateTime::fromIso('2024-06-18', $zone))->format('yyyy-MM-dd'), "\n";echo Report::dueDate(Core\Time\DateTime::fromIso('2024-12-31', $zone))->format('yyyy-MM-dd'), "\n";2024-07-012025-01-01