Index

cDatePeriod

MMO\core\helpers · 1 symbol · generated 30 Sep 2026

enum

cDatePeriod

cDatePeriod.php:15

A calendar period granularity (day, week, month, ...), with helpers to compute its boundaries around a given date, the difference between two dates in that unit, and to shift a date by N periods.

enum MMO\core\helpers\cDatePeriod: string
Namespace
MMO\core\helpers
[$start, $end] = cDatePeriod::Month->boundaries('2026-02'); // Feb 2026: 2026-02-01 00:00:00 -> 2026-02-28 23:59:59 cDatePeriod::Day->diff(new DateTime('2026-01-10'), new DateTime('2026-01-01')); // 9

Index

Day
Hour
Minute
Month
Second
Semester
Trimester
Week
Year
name readonly
value readonly
boundaries($date, $count)Get the boundaries of a period, using a date as a reference.
BoundariesFromString($period, $date) staticParse $period (FromString) then compute its boundaries.
cases() static
diff($date2, $date1)Compute the difference between first argument and second arguments (date2 - date1)
from($value) static
FromString($period) staticConvert a text like 3y to [cDatePeriod::Year, 3]
modify($date, $count)Shift $date by $count periods (via DateTime::modify's relative-format string).
tryFrom($value) static

Constants9

Day

const Day = MMO\core\helpers\cDatePeriod::Day

Hour

const Hour = MMO\core\helpers\cDatePeriod::Hour

Minute

const Minute = MMO\core\helpers\cDatePeriod::Minute

Month

const Month = MMO\core\helpers\cDatePeriod::Month

Second

const Second = MMO\core\helpers\cDatePeriod::Second

Semester

const Semester = MMO\core\helpers\cDatePeriod::Semester

Trimester

const Trimester = MMO\core\helpers\cDatePeriod::Trimester

Week

const Week = MMO\core\helpers\cDatePeriod::Week

Year

const Year = MMO\core\helpers\cDatePeriod::Year

Properties2

name

readonly
readonly string $name
Type
string

value

readonly
readonly string $value
Type
string

Methods8

boundaries($date, $count)

cDatePeriod.php:116

Get the boundaries of a period, using a date as a reference.

function boundaries(DateTimeInterface|string|null $date = null, int $count = 1): array
$date = nullDateTimeInterface | string | nullnull uses the current time; if string MUST be of the format: Y(year) | Y-m (month, trimester, semester) | Y-m-d (week, day) | any format dateTime accepts (hour, minute, second)
$count = 1intthe number of periods (can be negative)
returnsarray\DateTimeImmutable} [start, end]
if $date is '2026-03-18', 3 years boundaries will be: 2026-01-01 / 2028-12-31if $date is '2026-03-18', -3 years boundaries will be: 2024-01-01 / 2026-12-31cDatePeriod::Month->boundaries('2026-02'); // [2026-02-01 00:00:00, 2026-02-28 23:59:59] NOTE: count -1 and 1 are the same.

cases()

static
static function cases(): array
returnsarray

diff($date2, $date1)

cDatePeriod.php:58

Compute the difference between first argument and second arguments (date2 - date1)

function diff(DateTimeInterface|string|int $date2, DateTimeInterface|string|int $date1): int
cDatePeriod::Year->diff(new DateTime('2026-01-01'), new DateTime('2024-01-01')); // 2

from($value)

static
static function from(string|int $value): static
$valuestring | int
returnsstatic

FromString($period)

staticcDatePeriod.php:36

Convert a text like 3y to [cDatePeriod::Year, 3]

static function FromString(string $period): array
$periodstringa count followed by one of the enum's value letters ('d','w','m','T','S','y','h','i','s'); the count defaults to 1 if omitted (e.g. 'd' alone means 1 day)
returnsarray[cDatePeriod, int]
cDatePeriod::FromString('3y'); // [cDatePeriod::Year, 3]cDatePeriod::FromString('d'); // [cDatePeriod::Day, 1]

modify($date, $count)

cDatePeriod.php:83

Shift $date by $count periods (via DateTime::modify's relative-format string).

function modify(DateTimeInterface|string|int $date, int $count = 1): DateTimeInterface
$dateDateTimeInterface | string | int
$count = 1intnegative to go backward
returnsDateTimeInterfacea new instance if $date was int/string or already immutable; the SAME instance, modified in place, if $date is a mutable \DateTime
cDatePeriod::Month->modify(new DateTime('2026-01-15'), 2); // 2026-03-15

tryFrom($value)

static
static function tryFrom(string|int $value): ?static
$valuestring | int
returnsnull | static