CkDate
Job: read calendar days and moments out of the mixed shapes real data arrives in, compare, count and move them safely, and write them back out in the one shape you actually need.
A day is a date without a time of day: a birth date, a due date, the edge of a billing
period. A moment is a date and a time: when a record was created, when a token
expires, when a payment was taken. PHP has one date type, so one class holds both, and
every method here says which it answers. A day comes back as a CkDay — a Carbon at
midnight UTC that says it is a day — from ToCarbonDay, the Add family and the edges of
a week, a month, a quarter and a year; a moment comes back as a Carbon in UTC from
ToCarbon, AddHours and its siblings, StartOfDay and StartOfNextDay. The column type
picks the reader: a date column wants ToCarbonDay, a datetime column ToCarbon. The
questions follow the same line: IsPast, IsFuture, IsToday, IsBetween, IsWeekday
and the rest answer to the day — IsPast is false for anything dated today — where
IsBefore, IsAfter and SameDay answer to the moment: 09:00 is before 10:00 on the same
day.
Dates show up in many costumes: '2026-08-31', '8/31/2026 2:05 PM', '31.08.2026', a
bare 20260831, a Unix timestamp, an Excel day number, a date object out of a database
driver or a framework. CkDate reads them all with one set of rules — strict, never a
guess, the machine's locale and time zone never consulted. A value that cannot be read
answers the year-one sentinel, 0001-01-01T00:00:00Z as a Carbon — a date no real
record carries, so bad data stands out instead of hiding as today. The OrNull twins
answer null instead, which is what a nullable database column wants.
Every Carbon you get back is UTC
A date object carries its zone, and the one you get here is in UTC — always, the
sentinel included — so its answers are safe to compare and to store, whatever zone the
value arrived in: a value that declares an offset is converted, a date object is read
in the zone it carries, and zone-less digits count as UTC unless assumeZone names the
clock they were written on. The one deliberate exception is InZone, which
answers a Carbon carrying a zone, for display. And there is one switch, for an
application whose database stores local time: with CkSettings::DefaultAssumeZone
naming that zone, every moment is handed out carrying it instead — see
Your time zone, set once. The Carbon is the library's own
copy, SafeCoder\CommonKit\Packages\Carbon\Carbon: a PHP DateTime like any other, so
anything typed \DateTimeInterface takes it, and your application's own Carbon class
takes it as an argument — Carbon::instance($value) converts in one call.
Quick look
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbon('8/31/2026 2:05 PM'); // -> Carbon 2026-08-31T14:05:00Z (digits kept, counted as UTC)
CkDate::ToCarbonDay('8/31/2026 2:05 PM'); // -> CkDay 2026-08-31T00:00:00Z (the time is dropped: a day)
CkDate::ToCarbonOrNull('1900-01-01'); // -> null (an SQL placeholder, not a real date)
CkDate::ToCarbon('junk'); // -> Carbon 0001-01-01T00:00:00Z (rejected: the year-one sentinel)
CkDate::Iso8601('8/31/2026 2:05 PM'); // -> '2026-08-31T14:05:00Z'
CkDate::Unix('2026-08-31T14:05:00Z'); // -> 1788185100
CkDate::Format(
input: '8/31/2026',
into: 'd.m.Y'
); // -> '31.08.2026'
CkDate::ToCarbonDay(
input: '2026-09-01T01:00:00Z',
zone: 'America/Chicago'
); // -> CkDay 2026-08-31T00:00:00Z (the customer's day)
CkDate::IsBefore(
input: '2026-08-31T09:00:00Z',
other: '2026-08-31T10:00:00Z'
); // -> true (to the moment)
CkDate::IsPast(
input: '2026-08-31T13:05:00Z',
asOf: '2026-08-31T14:05:00Z'
); // -> false (to the day: still today)
CkDate::IsWeekend('2026-08-29'); // -> true (a Saturday)
CkDate::AddMonths(
input: '2026-01-31',
months: 1
); // -> CkDay 2026-02-28T00:00:00Z (never 3 March)
CkDate::AddWeekdays(
input: '2026-09-04',
days: 5
); // -> CkDay 2026-09-11T00:00:00Z (five business days)
CkDate::AddMinutes(
input: '2026-08-31T14:05:00Z',
minutes: 30
); // -> Carbon 2026-08-31T14:35:00Z
CkDate::StartOfDay(
input: '2026-08-31T14:05:00Z',
zone: 'America/Chicago'
); // -> Carbon 2026-08-31T05:00:00Z (where the customer's day began)
CkDate::TimeAgo(
input: '2026-08-28T12:00:00Z',
asOf: '2026-08-31T12:00:00Z'
); // -> '3 days ago'
CkDate::InZone(
input: '2026-08-31T14:05:00Z',
zone: 'America/Chicago'
)->format('Y-m-d H:i'); // -> '2026-08-31 09:05'
CkDate::Explain('5/6/26'); // -> 'a two-digit year is ambiguous'
Methods
ToCarbon and ToCarbonDay, with their OrNull twins, understand the same shapes as every
other method here: ISO 8601 text (the international standard, '2026-08-31' or
'2026-08-31T14:05:00Z'), bare 20260831 as text or number, slashed / dashed / dotted
numeric dates with an optional 24-hour or AM/PM time after the date, Unix timestamps
(seconds from the epoch to the end of year 9999; from a trillion up, milliseconds — a number
is never a negative moment, since -1 is what Unix answers for a value it cannot read),
Excel day numbers as a float (how Excel and older databases count days), and date objects
of any class — a DateTimeImmutable, a DateTime, a Carbon of any copy — in whatever zone
they carry. A date alone is midnight, UTC, to the moment methods, and that day on every
clock to the day methods.
The plain readers take a fallback to use when the value cannot be read — and the fallback
is checked the same way, so a placeholder fallback is no fallback. When both fail, you get
the year-one sentinel.
Each method below says what it answers: a day — a CkDay, a Carbon at midnight UTC —
or a moment — a Carbon in UTC.
ToCarbon
Read a full moment, as a Carbon in UTC.
static function ToCarbon(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text in any supported shape. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates — 0001-01-01, 1899-12-30, 1900-01-01, 2079-06-06, 9999-12-31 — as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
Moment to use when the value cannot be read. It is read in the zone it carries and checked the same way — a placeholder fallback is no fallback. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
The value (or the checked fallback) reads as a moment |
The moment, a Carbon in UTC: 2026-08-31T14:05:00Z |
The moment, a Carbon in UTC: 2026-08-31T14:05:00Z |
Rejected: missing, unreadable, an impossible date under the declared order, a placeholder read as missing, or a moment its own offset puts outside years 1 to 9999 ('0001-01-01T00:00:00+01:00' is the evening of year zero) — and no usable fallback |
Year one as a Carbon, 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Zone-less digits are declared UTC, never shifted — a value that declares its own zone always wins.
The reader every other method here reads through. It keeps the time of day and always
answers a Carbon in UTC (Coordinated Universal Time — the world's zero time zone, the one
payloads and databases should live in): a Carbon has every method a date needs — addDays,
diffInDays, startOfMonth, diffForHumans, ->age — so this is the value to carry around
and work on. Reading is strict and explicit, never a guess: 'tomorrow' is not a date, a
two-digit year is rejected rather than given a century, and '13/5/2026' read month first
is rejected, never quietly re-read day first. A date alone is that day at midnight, UTC.
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbon('8/31/2026 2:05 PM'); // -> Carbon 2026-08-31T14:05:00Z (digits kept, counted as UTC)
CkDate::ToCarbon('2026-08-31T14:05:00+02:00'); // -> Carbon 2026-08-31T12:05:00Z (its own zone wins: converted)
CkDate::ToCarbon(1788185100); // -> Carbon 2026-08-31T14:05:00Z (Unix seconds)
CkDate::ToCarbon('2026-08-31'); // -> Carbon 2026-08-31T00:00:00Z (a date alone is midnight, UTC)
assumeZone handles wall-clock data — values written from a local clock with no
zone attached. Name the zone the digits were written in, and they are converted to UTC.
A value that declares its own zone (an offset, a trailing Z, a date object, a Unix
timestamp) always wins; assumeZone never overrides it. Both IANA ids
('America/Chicago') and Windows ids ('Central Standard Time') work, on every operating
system — a Windows id is mapped by the intl extension; an old name ('US/Central',
'Europe/Kiev', 'CET') is the library's own knowledge of the zone it stands for, so it
reads the same on a host whose zone database leaves the legacy names out; and a zone's
daylight-saving history comes from the zone database PHP reads. A wall-clock
time the zone's clocks skip — the spring-forward hour — is rejected: someone wrote down a
time that was not on the clock. One that happens twice, when the clocks go back, reads as
standard time.
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbon(
input: '8/31/2026 20:30',
assumeZone: 'America/Chicago'
); // -> Carbon 2026-09-01T01:30:00Z (Central wall clock to UTC, crossing midnight)
CkDate::ToCarbon(
input: '2026-03-08 02:30',
assumeZone: 'America/Chicago'
); // -> Carbon 0001-01-01T00:00:00Z (the clocks skipped 02:30 that night: rejected)
Your time zone, set once
Two constants in CkSettings stand behind the two zone parameters, so an application that
lives in one zone says so in one place. Both ship as null, which means UTC, and a call can
always pass its own value — 'UTC' included.
| Constant | Stands behind | Decides |
|---|---|---|
CkSettings::DefaultZone |
every zone parameter |
Whose calendar day counts. What "today" is for IsToday, IsPast, IsFuture, Age, IsAdult; which day a moment belongs to in every method that answers or counts days; where StartOfDay falls; what InZone and SameDay use when told nothing. |
CkSettings::DefaultAssumeZone |
every assumeZone parameter |
Which clock a zone-less value was written on — and, once set, the clock every moment is handed out on. For a database that stores local time, and for no other. |
// no-check
// with DefaultZone = 'Europe/Bucharest'
CkDate::IsToday('2026-09-01'); // true from midnight in Bucharest: 21:00 UTC the evening before
CkDate::ToCarbonDay('2026-08-31T22:30:00Z'); // -> CkDay 2026-09-01T00:00:00Z (the day it was there)
CkDate::ToCarbonDay('2026-08-31'); // -> CkDay 2026-08-31T00:00:00Z (a date alone is never moved)
CkDate::StartOfDay(time()); // -> Bucharest's midnight, as a UTC moment
CkDate::InZone($order['created_at']); // -> a Carbon on Bucharest's wall clock
DefaultZone is safe to set: it changes which day an answer names and never a stored
value, and a date with no time of day is never moved by either zone.
DefaultAssumeZone — only when your database stores local time
A database gives a stored moment back with no zone — as text, '2026-10-05 09:00:00',
or as a date object a framework has already put on the application's zone. With
DefaultAssumeZone set to Bucharest, that value is read as Bucharest's clock. If the
database stores UTC, that is three hours wrong on every load: leave this null and
pass assumeZone: on the calls that read what people typed. If it stores local
time, this setting is exactly right.
A database that stores local time. With DefaultAssumeZone naming its zone, the library
works on that clock from end to end, and the zone is said nowhere else:
- In — a value with no zone, which is what the database hands back, is read as that zone's wall clock.
- Out — every moment that
ToCarbon,ToCarbonOrNull, the moment methods on this page andCkDictionary::Carbonon CkDictionary hand out is a Carbon carrying that zone instead of UTC: the same moment, its digits the ones the database holds, so->format('Y-m-d H:i:s')writes it back as it was and a value read and saved again never shifts. Afallbackis handed out on that clock too. The day methods still answer aCkDayat midnight UTC: a day has no clock to be on. - Written —
Formatwrites that clock, with the zone's own offset when the format asks for one (P,c,e);Iso8601andUnixstill write the true moment. - Placeholders —
'1900-01-01 00:00:00'or'2079-06-06 23:59:00'in a column is judged by its digits as stored, as any value with no zone is, so it is missing whichever way the zone would move it; and a date object standing on that zone is judged by the digits it shows.
// no-check
// with DefaultAssumeZone = 'America/Chicago' and DefaultZone = 'America/Chicago'
$created = CkDate::ToCarbon($row['created_at']); // stored '2026-10-05 09:00:00' -> a Carbon carrying America/Chicago
$created->format('Y-m-d H:i:s'); // -> '2026-10-05 09:00:00' (ready to save as it is)
CkDate::ToCarbon('2026-10-05T14:00:00Z')->format('H:i'); // -> '09:00' (that moment, on Chicago's clock)
CkDate::AddHours(
input: $row['created_at'],
hours: 1
)->format('H:i'); // -> '10:00'
CkDate::Iso8601($row['created_at']); // -> '2026-10-05T14:00:00Z' (the true moment, for the wire)
CkDate::Format(
input: $row['created_at'],
into: 'c'
); // -> '2026-10-05T09:00:00-05:00'
Three things to know there:
- Set
DefaultZoneto the same id, so "today" and the day a moment belongs to are that clock's as well. - "Now" on that clock is
CkDate::ToCarbon(time()). Compare a value the library handed out with that, or throughIsPast,IsBeforeand their siblings. A per-callassumeZonestill says how one value is read; it never changes the clock answers are handed out on. - Local time has one cost no library removes. When the clocks go back an hour happens twice, and its digits cannot say which of the two they were: they read as standard time. The Carbon handed out is exact; the text it is saved as is not, so a moment saved during the first of the two comes back an hour later. A database that stores UTC has no such hour.
The constants live in CkSettings, the one class of the library you edit: set them in your
copy.
ToCarbonOrNull
The same reader, answering null.
static function ToCarbonOrNull(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true
): ?Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text in any supported shape. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format the text must match, when one is declared. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
A time zone id for zone-less values, converted to UTC — see ToCarbon. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Reads SQL placeholder dates as missing, so they come back as null too. |
Returns
| When | You get |
|---|---|
| The value reads as a moment | The moment, a Carbon in UTC: 2026-09-02T10:30:00Z |
Missing (null or ''), unreadable, or a placeholder date read as missing |
null — this method never throws |
There is no fallback parameter — null is the answer for missing.
The honest version for nullable columns. It reads exactly like ToCarbon, but
answers null for anything that is not a real value: a missing input, one that cannot be
read, or — by default — an SQL placeholder date such as 1900-01-01. So a nullable
datetime column receives NULL rather than 0001-01-01, which a legacy SQL datetime column
cannot even hold.
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbonOrNull('2026-09-02T10:30:00Z'); // -> Carbon 2026-09-02T10:30:00Z
CkDate::ToCarbonOrNull('1900-01-01'); // -> null (a placeholder means "nobody entered anything")
CkDate::ToCarbonOrNull('junk'); // -> null
CkDate::ToCarbonOrNull(''); // -> null
This is the reader for pulling moments out of a payload bag with CkDictionary and for writing a model's properties before a save.
ToCarbonDay
Read a calendar day, as a CkDay.
static function ToCarbonDay(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text in any supported shape. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value carries a time of day and declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates — 0001-01-01, 1899-12-30, 1900-01-01, 2079-06-06, 9999-12-31 — as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
Date to use when the value cannot be read. It is checked the same way — a placeholder fallback is no fallback — and comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a date | The day, a CkDay at midnight UTC: 2026-08-31 |
The day: 2026-08-31 |
The value cannot be read and the checked fallback can |
The fallback, as given | The fallback, as given |
Rejected: missing, unreadable, an impossible date under the declared order, or a placeholder read as missing — and no usable fallback |
Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Two-digit years are rejected outright — never assigned a century by guess.
The day a date column holds, with no time of day. A time of day in the value is read,
used to place the moment, then dropped. An all-numeric date like '3/4/2026' is ambiguous
— March 4th or April 3rd? The order parameter decides, and month-first is the
default. For European input, pass DateOrder::DayFirst. There is no guessing: an
impossible date under the declared order is rejected, never quietly re-read the other way.
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\Enums\DateOrder;
CkDate::ToCarbonDay('2026-08-31'); // -> CkDay 2026-08-31T00:00:00Z
CkDate::ToCarbonDay('20260831'); // -> CkDay 2026-08-31T00:00:00Z (the compact shape)
CkDate::ToCarbonDay('3/4/2026'); // -> CkDay 2026-03-04T00:00:00Z (month-first by default)
CkDate::ToCarbonDay(
input: '3/4/2026',
order: DateOrder::DayFirst
); // -> CkDay 2026-04-03T00:00:00Z (declared day-first)
CkDate::ToCarbonDay('13/5/2026'); // -> Carbon 0001-01-01T00:00:00Z (no month 13: rejected, not re-read)
Whose day? A moment belongs to different days in different places, and the day you get
is the UTC one unless you say otherwise. assumeZone answers "which clock was this written
on"; zone answers "which day was this for the customer":
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbonDay('2026-09-01T01:00:00Z'); // -> CkDay 2026-09-01T00:00:00Z
CkDate::ToCarbonDay(
input: '2026-09-01T01:00:00Z',
zone: 'America/Chicago'
); // -> CkDay 2026-08-31T00:00:00Z (still the evening before, there)
CkDate::ToCarbonDay(
input: '2026-09-01',
zone: 'America/Chicago'
); // -> CkDay 2026-09-01T00:00:00Z (a date alone is never moved)
What a CkDay is. A Carbon at midnight UTC that says it is a day, from
SafeCoder\CommonKit\CkDay. It adds one question to Carbon and takes nothing away: every
Carbon method is there, anything typed \DateTimeInterface takes it, and
->format('Y-m-d') writes it. What it changes is how the library reads it back. A plain
date object at midnight UTC is a moment — the evening before in Chicago — so a method told
a zone places it there and answers for the day before. A CkDay is a date alone, as the
text '2026-08-31' is: that day on every clock, never moved by zone or assumeZone, with
no time of day to ask about. It stays a day for as long as its time of day is midnight —
moved by days, months or years it is still one — and IsDateAlone() says so; given a time
of day, or put on another zone's clock, it is the moment it then shows. A copy made by
another date class — your application's own Carbon, a DateTimeImmutable — or the text
JSON makes of it is a plain moment again, so where a day has to survive such a trip, hand
it on as ->format('Y-m-d').
use SafeCoder\CommonKit\CkDate;
$day = CkDate::ToCarbonDay('2026-08-29'); // -> CkDay 2026-08-29T00:00:00Z (a Saturday)
$day->IsDateAlone(); // -> true
$day->format('Y-m-d'); // -> '2026-08-29'
CkDate::IsWeekend(
input: $day,
zone: 'America/Chicago'
); // -> true (that day, on every clock)
$moment = new \DateTimeImmutable('2026-08-29 00:00:00', new \DateTimeZone('UTC'));
CkDate::IsWeekend(
input: $moment,
zone: 'America/Chicago'
); // -> false (a plain date object is a moment: Friday evening there)
CkDate::ToCarbon(
input: $day,
assumeZone: 'America/Chicago'
); // -> Carbon 2026-08-29T05:00:00Z (a moment method takes it as midnight on that clock)
ToCarbonDayOrNull
The same day reader, answering null.
static function ToCarbonDayOrNull(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true
): ?CkDay
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text in any supported shape. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format the text must match, when one is declared. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
A time zone id for zone-less values that carry a time of day — see ToCarbon. A date alone is never moved by it. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts for a value that carries a time of day; null for the UTC day. A date alone is that day everywhere. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Reads SQL placeholder dates as missing, so they come back as null too. |
Returns
| When | You get |
|---|---|
| The value reads as a date | The day, a CkDay at midnight UTC: 1985-04-12 |
Missing (null or ''), unreadable, or a placeholder date read as missing |
null — this method never throws |
There is no fallback parameter — null is the answer for missing.
The honest version for a nullable date column, or a form field that may be left empty. It
reads exactly like ToCarbonDay, but answers null for anything that is not
a real value.
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbonDayOrNull('1985-04-12'); // -> CkDay 1985-04-12T00:00:00Z
CkDate::ToCarbonDayOrNull('1900-01-01'); // -> null (a placeholder means "nobody entered anything")
CkDate::ToCarbonDayOrNull('junk'); // -> null
CkDate::ToCarbonDayOrNull(
input: '1900-01-01',
treatSentinelsAsMissing: false
); // -> CkDay 1900-01-01T00:00:00Z (you declared placeholders real here)
This is the reader for pulling days out of a payload bag with
CkDictionary and for writing a model's date properties before a save.
Iso8601
The one true timestamp shape — a moment.
static function Iso8601(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
mixed $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format for reading, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
mixed |
null |
Value to use when the input cannot be read. It goes through the same reading, never returned as typed; a fallback that is itself rejected answers ''. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering '', when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
The value (or the checked fallback) reads as a moment |
The ISO 8601 UTC text: '2026-08-31T00:00:00Z' |
The ISO 8601 UTC text: '2026-08-31T00:00:00Z' |
Rejected: missing, unreadable, or a placeholder read as missing — the fallback too |
Empty string | Throws CkDateException — carries Input, Expected and Reason |
A fallback goes through the same reading first, never echoed as typed.
Writes any readable value as ISO 8601 UTC text — '2026-08-31T14:05:00Z' — the shape
every payload and API timestamp should use. A day — a CkDay, or '2026-08-31' — is
written as its midnight; for the day alone, a JSON date field or an SQL date literal,
write it with Format and into: 'Y-m-d'. Rejected values answer '' (empty
text); a fallback value goes through the same reading first, never echoed as typed.
use SafeCoder\CommonKit\CkDate;
CkDate::Iso8601('8/31/2026'); // -> '2026-08-31T00:00:00Z'
CkDate::Iso8601('2026-08-31T14:05:00+02:00'); // -> '2026-08-31T12:05:00Z' (converted to UTC)
CkDate::Iso8601('junk'); // -> ''
Format
Write any other shape, in a PHP date format — a moment.
static function Format(
mixed $input = null,
?string $into = 'Y-m-d H:i:s',
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
mixed $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
into |
?string |
'Y-m-d H:i:s' |
The PHP date format to write, e.g. 'm/d/Y' or 'd.m.Y H:i'. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format for reading, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
mixed |
null |
Value to use when the input cannot be read. It goes through the same reading, never returned as typed; a fallback that is itself rejected answers ''. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering '', when a value cannot be used or no into is given. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
The value (or the checked fallback) reads, and into names a format |
The text in the into shape: '08/31/2026 14:05' |
The text in the into shape: '08/31/2026 14:05' |
Rejected: missing, unreadable, a placeholder read as missing, or no into at all |
Empty string | Throws CkDateException — carries Input, Expected and Reason |
Month and day names come out in English, whatever the machine's locale.
For display, file names, legacy feeds and database columns: 'Y-m-d H:i:s' (the default)
for a DATETIME column, 'Y-m-d' for a DATE column, 'H:i:s' for a TIME column. into and
format are PHP's own date formats — the ones date(), DateTime and Carbon use — so
there is no second format language to learn. Every text is a PHP format, a letter that is
no format character written as it is, so the one thing that can go wrong on the way out
is no format at all: an empty into answers '', with the reason "no output format". The
default writes the whole moment, so a time of day is never dropped unasked; a day comes
out at midnight. The clock written is UTC — or the database's own, where
CkSettings::DefaultAssumeZone names it, a format that writes
an offset or a zone (P, c, e) writing that zone's own. A reading format is strict
where PHP's own parser is lenient: '31.02.2026' read with 'd.m.Y' does not roll into
March. In a reading format the characters \, |, ?, *, +, ! and # mean
something to PHP — escape them to match them.
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\Enums\DateOrder;
CkDate::Format('junk'); // -> '' (rejected)
CkDate::Format('8/31/2026 2:05 PM'); // -> '2026-08-31 14:05:00' (the default writes the whole moment)
CkDate::Format(
input: '2026-08-31T14:05:00Z',
into: 'm/d/Y H:i'
); // -> '08/31/2026 14:05'
CkDate::Format(
input: 20260831,
into: 'd.m.Y'
); // -> '31.08.2026'
CkDate::Format(
input: '1/2/2026',
into: 'F j, Y'
); // -> 'January 2, 2026' (always English)
CkDate::Format(
input: '2026-08-31',
into: 'l, j F Y'
); // -> 'Monday, 31 August 2026'
CkDate::Format(
input: '31.08.2026',
format: 'd.m.Y',
into: 'Y-m-d'
); // -> '2026-08-31' (read in one exact shape, written in another)
CkDate::Format(
input: '31.02.2026',
format: 'd.m.Y'
); // -> '' (30 days hath February: no rolling into March)
Unix
Seconds since 1970 — a moment.
static function Unix(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
int $fallback = -1,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
-1 |
What comes back when the value cannot be read — pick another when pre-1970 moments are real data. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of returning the fallback, when the value cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a moment | The Unix seconds: 1788185100 |
The Unix seconds: 1788185100 |
| Rejected: missing, unreadable, or a placeholder read as missing | The fallback (default -1) |
Throws CkDateException — carries Input, Expected and Reason |
A pre-1970 moment is a valid negative — it can collide with the default -1.
Writes a value as Unix time: whole seconds since 1970-01-01T00:00:00Z, the timestamp
shape most JSON APIs and payment processors use. Fractional seconds are floored. A
moment before 1970 comes back negative — which is valid Unix time, so if pre-1970
moments are real data for you, pick a different fallback than the default -1 —
and know that the readers never read a negative number as a moment, for that very
reason. From the epoch to the end of year 9999, what Unix writes,
ToCarbon reads back.
use SafeCoder\CommonKit\CkDate;
CkDate::Unix('1970-01-02'); // -> 86400
CkDate::Unix('2026-08-31T14:05:00Z'); // -> 1788185100
CkDate::Unix('junk'); // -> -1 (the default fallback)
Age
Whole years between two dates — by the day.
static function Age(
mixed $birth = null,
mixed $asOf = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
int $fallback = -1,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
birth |
mixed |
null |
The birth date: a date object, a number, or text in any supported shape. |
asOf |
mixed |
null |
The date to measure at, read the same way. null means today (UTC) — pass a value for reproducible results and tests. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
-1 |
What comes back when the age cannot be computed — an age nobody has. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of returning the fallback, when a date cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both dates read, and the birth date is not after the measuring date | The age in whole years: 36 |
The age in whole years: 36 |
Rejected: an unreadable birth or asOf, or a birth date after the measuring date |
The fallback (default -1) |
Throws CkDateException — carries Input, Expected and Reason |
A birth date after the measuring date is bad data, not a negative age.
The age in whole years at asOf — or at today (UTC) when you give no asOf. That
"today" is the one impure spot in the class; pass an asOf for reproducible results
and tests. Bad input answers the fallback, -1 by default — an age nobody has. A
birth date after the measuring date is bad data, not a negative age. Someone born on
29 February has their birthday on 1 March in a year without one.
use SafeCoder\CommonKit\CkDate;
CkDate::Age(
birth: '8/31/1990',
asOf: '8/31/2026'
); // -> 36 (the birthday itself counts)
CkDate::Age(
birth: '2/29/2000',
asOf: '2/28/2026'
); // -> 25 (leap-day birthday, not yet reached)
CkDate::Age('junk'); // -> -1 (the default fallback)
IsBetween
Is a date inside a range — by the day.
static function IsBetween(
mixed $input = null,
mixed $min = null,
mixed $max = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The date to test. |
min |
mixed |
null |
The lower bound, inclusive. |
max |
mixed |
null |
The upper bound, inclusive. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing — for input only; the bounds are always taken literally. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says which value is invalid and why, instead of returning false. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
All three read as dates and input lies inside the bounds, both ends included |
true |
true |
All three read as dates and input lies outside the bounds |
false |
false |
| Rejected: an unreadable value or bound, or bounds that contradict each other | false |
Throws CkDateException — carries Input, Expected and Reason |
The bounds never treat placeholder dates as missing, so '9999-12-31' works as an open upper bound.
True when the date lies between min and max, inclusive on both ends — "is this
service date inside the coverage period". By calendar day, in the zone: a moment on the
last day of the period is inside it whatever its time of day. False for an unreadable
value or bound, and for bounds that contradict each other. The bounds are always taken
literally — they never treat placeholder dates as missing — so '9999-12-31' works as an
open upper bound. To compare moments, IsBefore and IsAfter.
use SafeCoder\CommonKit\CkDate;
CkDate::IsBetween(
input: '6/15/2026',
min: '1/1/2026',
max: '12/31/2026'
); // -> true
CkDate::IsBetween(
input: '12/31/2026',
min: '1/1/2026',
max: '12/31/2026'
); // -> true (both ends count)
CkDate::IsBetween(
input: '2026-08-31T15:00:01Z',
min: '2026-08-30',
max: '2026-08-31'
); // -> true (by the day: 31 August, whatever the time)
CkDate::IsBetween(
input: '2026-06-15',
min: '2026-01-01',
max: '9999-12-31'
); // -> true (an open-ended bound is taken literally)
IsDate
Does it read as a date at all?
static function IsDate(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value cannot be read. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a date under the readers' rules | true |
true |
| Rejected: unreadable, an impossible date in the declared order, a two-digit year, a placeholder | false |
Throws CkDateException — carries Input, Expected and Reason |
The yes-or-no form of ToCarbon and ToCarbonDay: any supported
shape, strict, never a guess, the placeholder dates counting as missing by default. A
moment reads — '2026-08-31T14:05:00Z' is a date — and so does a date alone;
HasTime tells the two apart. When the value is going to be stored, read it with
an OrNull reader instead and check for null — that answers the same question and hands
you the value.
use SafeCoder\CommonKit\CkDate;
CkDate::IsDate('8/31/2026'); // -> true
CkDate::IsDate(20260831); // -> true
CkDate::IsDate('2026-08-31T14:05:00Z'); // -> true (a moment is a date too)
CkDate::IsDate('13/5/2026'); // -> false (impossible month-first; never re-read)
CkDate::IsDate('1900-01-01'); // -> false (a placeholder)
CkDate::IsDate('14:05'); // -> false (a time alone is no date)
IsPlaceholder
Is it one of the "nobody entered anything" dates?
static function IsPlaceholder(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value is not a date at all. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a date and it is 0001-01-01, 1899-12-30, 1900-01-01, 2079-06-06 or 9999-12-31 | true |
true |
| Any other date | false |
false |
| Rejected: not a date | false |
Throws CkDateException — carries Input, Expected and Reason |
The readers turn the placeholder dates into "missing" silently; this method says so out
loud. For data-quality checks — "how many rows hold 1900-01-01" — and for deciding what
a source really sent. There is no treatSentinelsAsMissing here: the placeholders must
read as dates, that being the whole question. The year-one sentinel the readers answer
is one of them, so this is also how a rejected value is found again.
use SafeCoder\CommonKit\CkDate;
CkDate::IsPlaceholder('1900-01-01'); // -> true
CkDate::IsPlaceholder('1/1/1900'); // -> true
CkDate::IsPlaceholder(2.0); // -> true (Excel day 2 is 1900-01-01)
CkDate::IsPlaceholder('2026-08-31'); // -> false
CkDate::IsPlaceholder(CkDate::ToCarbon('junk')); // -> true (the year-one sentinel is one)
IsTime
Does it read as a time of day?
static function IsTime(
mixed $input = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, or text. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value cannot be read. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a time of day — midnight included | true |
true |
| Rejected: out of range, minutes missing, a bare number, not a time | false |
Throws CkDateException — carries Input, Expected and Reason |
The time check. PHP has no type for a time of day alone, so there is no reader for one:
this method checks the text, and to store a value for a TIME column keep the text it has
checked, or read the value with ToCarbon when it comes with its date and
write it with Format and into: 'H:i:s'. The shapes understood: '14:05',
'14:05:30', '2:05 PM' (with or without the space), '10pm', 'T14:05', the compact
'1405' and '140530', and the time part of a date object — a CkDay has none. A bare
number is never a time of day — 1405 the number could mean too many things.
use SafeCoder\CommonKit\CkDate;
CkDate::IsTime('2:05 PM'); // -> true
CkDate::IsTime('00:00'); // -> true (midnight is real)
CkDate::IsTime('140530'); // -> true (14:05:30)
CkDate::IsTime('25:00'); // -> false
CkDate::IsTime(1405); // -> false (a number is not a time)
IsIso8601
Is it written as ISO 8601 text?
static function IsIso8601(
mixed $input = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The raw value. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value is not ISO 8601 date text. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Text in one of the ISO 8601 shapes the readers accept, and a real calendar date — the moment it names, its offset applied, within years 1 to 9999 | true |
true |
Rejected: another date shape, a number, a date object, ISO-shaped text that is not a date or whose offset moves it outside the calendar ('0001-01-01T00:00:00+01:00') |
false |
Throws CkDateException — carries Input, Expected and Reason |
A shape check for API contracts and feeds that must carry ISO text — the same idea as
CkCountry::IsAlpha2. The accepted shapes are the extended ones:
'2026-08-31', '2026-08-31T14:05:00Z', '2026-08-31T14:05:00.123+02:00',
'2026-08-31 14:05', and the space form with seconds, fractions or a zone as SQL exports
write it, '2026-08-31 14:05:30.123', '2026-08-31 14:05+02:00'. A zone belongs to a time
of day, so a date alone with one, '2026-08-31Z', is not ISO text the readers take. '8/31/2026' and
20260831 are dates but not ISO text; '2026-02-30' is ISO-shaped but not a date.
Whether the date means anything is IsDate's question: a placeholder like
'1900-01-01' is still ISO text.
use SafeCoder\CommonKit\CkDate;
CkDate::IsIso8601('2026-08-31T14:05:00Z'); // -> true
CkDate::IsIso8601('2026-08-31 14:05'); // -> true (the space form, as SQL writes it)
CkDate::IsIso8601('8/31/2026'); // -> false (a date, not ISO)
CkDate::IsIso8601('2026-02-30'); // -> false (ISO-shaped, not a date)
CkDate::IsIso8601('1900-01-01'); // -> true (a placeholder, but ISO text)
CkDate::IsIso8601('2026-08-31 14:05+02:00'); // -> true (the space form, with a zone)
CkDate::IsIso8601('2026-08-31Z'); // -> false (a date alone takes no zone)
HasTime
Does it say when, or only which day?
static function HasTime(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'. When given, text must match it and nothing else is tried; the value has a time when the format reads an hour, a minute or a second. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value is not a date at all. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value is a date that declares a time of day | true |
true |
| The value is a date without one | false |
false |
| Rejected: not a date | false |
Throws CkDateException — carries Input, Expected and Reason |
Judged by the shape of the value, not its digits. A plain date object always carries a
time — a day alone is a CkDay — and so does text with a clock in it,
'2026-08-31T00:00:00Z' included, which says midnight rather than nothing. A Unix
timestamp is a moment; an Excel day number has a time when it has a fraction.
'2026-08-31', 20260831 and a CkDay do not. There is no assumeZone: a zone changes
which moment the digits mean, never whether they include a clock. Useful for choosing
between a date and a date-time display, and for a field that must say when.
use SafeCoder\CommonKit\CkDate;
CkDate::HasTime('2026-08-31T14:05:00Z'); // -> true
CkDate::HasTime('8/31/2026 2:05 PM'); // -> true
CkDate::HasTime('2026-08-31'); // -> false
CkDate::HasTime(CkDate::ToCarbonDay('2026-08-31')); // -> false (a CkDay is a day)
CkDate::HasTime(new \DateTimeImmutable('2026-08-31')); // -> true (a plain date object always carries a time)
IsBefore
Does the start precede the end — to the moment?
static function IsBefore(
mixed $input = null,
mixed $other = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The moment to test. |
other |
mixed |
null |
The moment it is compared with. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing — for both values. To compare against an open-ended bound such as 9999-12-31, use IsBetween, whose bounds are always literal. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when either value cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
Both read and input is an earlier moment than other |
true |
true |
Both read, and input is the same moment or later |
false |
false |
| Rejected: either value cannot be read | false |
Throws CkDateException — carries Input, Expected and Reason |
Compared as moments, strictly: 09:00 is before 10:00 on the same day, and two readings of the same moment are neither before nor after each other. A date without a time is midnight, UTC. For "earlier than today" see IsPast, which compares calendar days. A placeholder on either side is missing, not a bound — switch the flag off, or use IsBetween, for an open-ended 9999-12-31.
use SafeCoder\CommonKit\CkDate;
CkDate::IsBefore(
input: '2026-08-30',
other: '2026-08-31'
); // -> true
CkDate::IsBefore(
input: '2026-08-31T09:00:00Z',
other: '2026-08-31T10:00:00Z'
); // -> true (the same day, an earlier moment)
CkDate::IsBefore(
input: '2026-08-31',
other: '2026-08-31'
); // -> false (the same moment)
$form = ['start' => '2026-08-31T09:00:00Z', 'end' => '2026-08-31T17:00:00Z'];
CkDate::IsBefore(
input: $form['start'],
other: $form['end'],
throwIfInvalid: true
); // -> true (the "start before end" check, an exception when either is not a date)
IsAfter
Does the end follow the start — to the moment?
static function IsAfter(
mixed $input = null,
mixed $other = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The moment to test. |
other |
mixed |
null |
The moment it is compared with. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing — for both values. To compare against an open-ended bound such as 9999-12-31, use IsBetween, whose bounds are always literal. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when either value cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
Both read and input is a later moment than other |
true |
true |
Both read, and input is the same moment or earlier |
false |
false |
| Rejected: either value cannot be read | false |
Throws CkDateException — carries Input, Expected and Reason |
The mirror of IsBefore, with the same rules: moments, strictly. For "later than today" see IsFuture, which compares calendar days.
use SafeCoder\CommonKit\CkDate;
CkDate::IsAfter(
input: '2026-08-31',
other: '2026-08-30'
); // -> true
CkDate::IsAfter(
input: '8/31/2026 2:05 PM',
other: '8/31/2026'
); // -> true (a date alone is midnight)
SameDay
Do two moments fall on the same calendar day?
static function SameDay(
mixed $input = null,
mixed $other = null,
?string $zone = CkSettings::DefaultZone,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The first moment. |
other |
mixed |
null |
The moment it is compared with. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id. null for UTC days. An id the library does not know counts as UTC. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when either value cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both read, same day | true |
true |
| Both read, different days | false |
false |
| Rejected: unreadable | false |
Throws CkDateException — carries Input, Expected and Reason |
"The same day" depends on whose day. 23:30 and 00:30 UTC are two days in London and one
evening in New York, so name the customer's zone when the question is theirs. Two missing
values are not the same day. With an asOf, IsToday asks the same of two
dates read as days.
use SafeCoder\CommonKit\CkDate;
CkDate::SameDay(
input: '2026-08-31T23:30:00Z',
other: '2026-09-01T00:30:00Z'
); // -> false (two UTC days)
CkDate::SameDay(
input: '2026-08-31T23:30:00Z',
other: '2026-09-01T00:30:00Z',
zone: 'America/New_York'
); // -> true
IsPast
Is it before today — by the day?
static function IsPast(
mixed $input = null,
mixed $asOf = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The date to test. |
asOf |
mixed |
null |
The day to compare with, read the same way. null means today (UTC) — pass a value for reproducible results and tests. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when a date cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
Both read and input falls on an earlier calendar day than asOf |
true |
true |
Both read, and input is the same day or later |
false |
false |
| Rejected: either date cannot be read | false |
Throws CkDateException — carries Input, Expected and Reason |
A birth date, a service already rendered, an expiry gone by. The comparison is by
calendar day, in UTC: a value dated today is neither past nor future, whatever its time
of day — a token that expired an hour ago is not past here. "Today" is UTC, the same as
Age; pass asOf for a local today, or name the zone. To compare moments, use
IsBefore.
use SafeCoder\CommonKit\CkDate;
CkDate::IsPast(
input: '8/30/2026',
asOf: '8/31/2026'
); // -> true
CkDate::IsPast(
input: '8/31/2026',
asOf: '8/31/2026'
); // -> false (today is not the past)
CkDate::IsPast(
input: '2026-08-31T13:05:00Z',
asOf: '2026-08-31T14:05:00Z'
); // -> false (an hour ago, but the same day)
CkDate::IsPast('2000-01-01'); // -> true (against today)
IsFuture
Is it after today — by the day?
static function IsFuture(
mixed $input = null,
mixed $asOf = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The date to test. |
asOf |
mixed |
null |
The day to compare with, read the same way. null means today (UTC) — pass a value for reproducible results and tests. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when a date cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
Both read and input falls on a later calendar day than asOf |
true |
true |
Both read, and input is the same day or earlier |
false |
false |
| Rejected: either date cannot be read | false |
Throws CkDateException — carries Input, Expected and Reason |
An expiry still ahead, a booking not yet due. Same rules as IsPast: calendar
days, UTC, today is neither. A placeholder such as 9999-12-31 is missing by default; pass
treatSentinelsAsMissing: false when it means "never expires". To compare moments, use
IsAfter.
use SafeCoder\CommonKit\CkDate;
CkDate::IsFuture(
input: '9/1/2026',
asOf: '8/31/2026'
); // -> true
CkDate::IsFuture(
input: '2026-08-31T23:59:59Z',
asOf: '8/31/2026'
); // -> false (still today)
IsToday
Is it today?
static function IsToday(
mixed $input = null,
mixed $asOf = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The date to test. |
asOf |
mixed |
null |
The day to compare with, read the same way. null means today (UTC) — pass a value for reproducible results and tests. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when a date cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both read and fall on the same calendar day | true |
true |
| Both read, on different days | false |
false |
| Rejected: either date cannot be read | false |
Throws CkDateException — carries Input, Expected and Reason |
The appointment is today, the record was created today. Calendar days in UTC, so the
time of day never matters. With an explicit asOf it is a "same day" check between any
two dates; SameDay asks the same of two moments in a named zone.
use SafeCoder\CommonKit\CkDate;
CkDate::IsToday(time()); // -> true (this very moment, as a Unix timestamp)
CkDate::IsToday(
input: '2026-08-31T23:59:59Z',
asOf: '8/31/2026'
); // -> true
IsAdult
Old enough?
static function IsAdult(
mixed $birth = null,
mixed $asOf = null,
int $minAge = CkSettings::DefaultAdultAge,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
birth |
mixed |
null |
The birth date: a date object, a number, or text in any supported shape. |
asOf |
mixed |
null |
The date to measure at, read the same way. null means today (UTC) — pass a value for reproducible results and tests. |
minAge |
int |
CkSettings::DefaultAdultAge (18) |
The age, in whole years, from which a person counts as an adult. Below 0 acts as 0. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when a date cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
Both dates read and the age in whole years is at least minAge |
true |
true |
Both read, and the age is below minAge |
false |
false |
| Rejected: an unreadable date, or a birth date after the as-of date | false |
Throws CkDateException — carries Input, Expected and Reason |
The arithmetic of Age, as a yes or no. The birthday itself counts. A birth date
after the as-of date is bad data, not a minor, so it is rejected rather than answered
false quietly — the flag makes it say so.
use SafeCoder\CommonKit\CkDate;
CkDate::IsAdult(
birth: '8/31/2008',
asOf: '8/31/2026'
); // -> true (18 today)
CkDate::IsAdult(
birth: '9/1/2008',
asOf: '8/31/2026'
); // -> false (tomorrow)
CkDate::IsAdult(
birth: '8/31/2006',
asOf: '8/31/2026',
minAge: 21
); // -> false (20)
IsWeekday
Monday to Friday?
static function IsWeekday(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value cannot be read. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a date on a Monday to Friday | true |
true |
| A Saturday or Sunday | false |
false |
| Rejected: not a date | false |
Throws CkDateException — carries Input, Expected and Reason |
The working days, for delivery, settlement and scheduling checks. The day is the UTC
calendar day of the moment read — or the zone's — and a zone-less date is taken as
written. Holidays are not known here — they differ by country and change — so that list
stays in your application. IsWeekend is the sibling; both answer false for
an unreadable value, so neither is the other's negation.
use SafeCoder\CommonKit\CkDate;
CkDate::IsWeekday('2026-08-31'); // -> true (a Monday)
CkDate::IsWeekday('2026-08-29'); // -> false (a Saturday)
IsWeekend
Saturday or Sunday?
static function IsWeekend(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value cannot be read. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads as a date on a Saturday or Sunday | true |
true |
| A weekday | false |
false |
| Rejected: not a date | false |
Throws CkDateException — carries Input, Expected and Reason |
Saturday and Sunday, the ISO 8601 week's rest days — what "weekend" means here
regardless of country. Same rules as IsWeekday. Note that with assumeZone
alone the UTC day decides: Friday 22:00 in Chicago is Saturday 03:00 UTC, and a weekend —
name the zone as well for Chicago's own Friday.
use SafeCoder\CommonKit\CkDate;
CkDate::IsWeekend('2026-08-29'); // -> true
CkDate::IsWeekend('junk'); // -> false (and so is IsWeekday)
CkDate::IsWeekend(
input: '2026-08-28 22:00',
assumeZone: 'America/Chicago'
); // -> true (Friday evening there is Saturday 03:00 UTC)
CkDate::IsWeekend(
input: '2026-08-28 22:00',
assumeZone: 'America/Chicago',
zone: 'America/Chicago'
); // -> false (Chicago's own Friday)
IsLeapYear
Is it a leap year?
static function IsLeapYear(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): bool
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a year on its own, a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads: month first or day first. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'. When given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering false, when the value is neither a year nor a date. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| A bare year, or the year of a date the readers accept, is a leap year | true |
true |
| It is not | false |
false |
| Rejected: neither a year nor a date | false |
Throws CkDateException — carries Input, Expected and Reason |
2024 yes, 2026 no, 1900 no, 2000 yes. The value may be a year on its own — 2024,
'2024' — or any date the readers accept, whose year is taken. A bare whole number from
1 to 9999 is a year here, not Unix seconds: nobody asks whether the first hours of 1970
were a leap year.
use SafeCoder\CommonKit\CkDate;
CkDate::IsLeapYear(2024); // -> true
CkDate::IsLeapYear('2/29/2024'); // -> true
CkDate::IsLeapYear('1900'); // -> false (a century, not divisible by 400)
DaysBetween
Whole days from one date to another.
static function DaysBetween(
mixed $from = null,
mixed $to = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
int $fallback = -2147483648,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
from |
mixed |
null |
The earlier date, or the later one for a negative answer. |
to |
mixed |
null |
The date to count to. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
-2147483648 |
What comes back when a date cannot be read — the lowest 32-bit integer, a distance no two dates are apart, and one that still fits an INT column. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the fallback, when a date cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both dates read | The signed number of calendar days, the time of day ignored | The days |
| Rejected: either date cannot be read | The fallback |
Throws CkDateException — carries Input, Expected and Reason — Expected names which date |
The companion of Age, which counts years. "Days until the deadline", "days since the last order", "nights of a stay". Calendar days, not 24-hour spans: 23:00 to 01:00 the next day is one day.
use SafeCoder\CommonKit\CkDate;
CkDate::DaysBetween(
from: '1/1/2026',
to: '1/31/2026'
); // -> 30
CkDate::DaysBetween(
from: '1/31/2026',
to: '1/1/2026'
); // -> -30
CkDate::DaysBetween(
from: '2026-08-31T23:00:00Z',
to: '2026-09-01T01:00:00Z'
); // -> 1 (calendar days, not hours)
CkDate::DaysBetween(
from: 'junk',
to: '1/1/2026'
); // -> -2147483648
Add
Years, months and days, in one move — a day.
static function Add(
mixed $input = null,
int $years = 0,
int $months = 0,
int $days = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
years |
int |
0 |
Years to add; negative to go back. |
months |
int |
0 |
Months to add; negative to go back. |
days |
int |
0 |
Days to add, after the years and months; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The day reached, a CkDay |
The day |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Read the way ToCarbonDay reads a value — a time of day in it places the
moment, then is gone. The years and months are added together, as months, and the day is
held to the end of the month when the month reached is shorter: the 31st plus one
month is the 28th, 29th or 30th — never the 3rd of the month after, which is what adding
"a month" by hand, or Carbon's own addMonths(), gives. The days are added last. One rule
follows from holding the day: the end of the month is not sticky — 28 February plus one
month is 28 March, not the 31st.
AddDays, AddMonths and AddYears are the same move with one unit each.
use SafeCoder\CommonKit\CkDate;
CkDate::Add(
input: '2026-01-31',
months: 1
); // -> CkDay 2026-02-28T00:00:00Z
CkDate::Add(
input: '2024-02-29',
years: 1
); // -> CkDay 2025-02-28T00:00:00Z
CkDate::Add(
input: '2026-01-31',
months: 1,
days: 1
); // -> CkDay 2026-03-01T00:00:00Z (28 February, then one more day)
CkDate::Add(
input: '2026-08-31',
days: -30
); // -> CkDay 2026-08-01T00:00:00Z
CkDate::Add(
input: '9999-06-15',
years: 1
); // -> Carbon 0001-01-01T00:00:00Z (rejected: outside the calendar)
AddDays
A number of days on, or back — a day.
static function AddDays(
mixed $input = null,
int $days = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
days |
int |
0 |
Days to add; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The day reached, a CkDay |
The day |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Calendar days, answered as a day: a moment given here is placed in the zone, taken as
its day, and the day reached comes back without the time. For days that skip weekends and
holidays see AddWeekdays; for exactly 24 hours on a moment,
AddHours.
use SafeCoder\CommonKit\CkDate;
CkDate::AddDays(
input: '2026-08-31',
days: 30
); // -> CkDay 2026-09-30T00:00:00Z
CkDate::AddDays(
input: '2026-08-31',
days: -30
); // -> CkDay 2026-08-01T00:00:00Z
CkDate::AddDays(
input: '2026-03-07T15:00:00Z',
days: 1
); // -> CkDay 2026-03-08T00:00:00Z (the day after, the time gone)
AddMonths
A number of months on, the day held to the month's end — a day.
static function AddMonths(
mixed $input = null,
int $months = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
months |
int |
0 |
Months to add; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The day reached, a CkDay |
The day |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
A subscription started on the 31st renews on the last day of a shorter month, never in the
month after it — Carbon's plain addMonths() would answer 3 March. See Add for the
rule in full.
use SafeCoder\CommonKit\CkDate;
CkDate::AddMonths(
input: '2026-01-31',
months: 1
); // -> CkDay 2026-02-28T00:00:00Z
CkDate::AddMonths(
input: '2026-03-31',
months: -1
); // -> CkDay 2026-02-28T00:00:00Z
CkDate::AddMonths(
input: '2026-08-15',
months: 12
); // -> CkDay 2027-08-15T00:00:00Z
AddYears
A number of years on — a day.
static function AddYears(
mixed $input = null,
int $years = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
years |
int |
0 |
Years to add; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The day reached, a CkDay |
The day |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
A leap day lands on 28 February in a year without one.
use SafeCoder\CommonKit\CkDate;
CkDate::AddYears(
input: '2024-02-29',
years: 1
); // -> CkDay 2025-02-28T00:00:00Z
CkDate::AddYears(
input: '2024-02-29',
years: 4
); // -> CkDay 2028-02-29T00:00:00Z
AddWeekdays
Business days on: weekends and your holidays stepped over — a day.
static function AddWeekdays(
mixed $input = null,
int $days = 0,
mixed $holidays = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
days |
int |
0 |
Weekdays to add; negative to go back. At most CkSettings::MaxWeekdaysToAdd (10,000) either way. |
holidays |
mixed |
null |
Days that do not count: one date, or an array of them, each read like the value itself. null for none. One that cannot be read rejects the call; at most CkSettings::MaxHolidays (5,000). |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The weekday reached, a CkDay |
The day |
| Rejected: unreadable, a holiday that cannot be read, more than 10,000 weekdays, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Monday to Friday count; Saturday, Sunday and the days you name in holidays do not.
CommonKit knows no country's holidays — they differ by country, by region and by year,
so they are your list, from your database or your configuration. Zero days answers the date
as it is, a Saturday included. A holiday that cannot be read rejects the call: one silently
dropped would move a due date.
use SafeCoder\CommonKit\CkDate;
CkDate::AddWeekdays(
input: '2026-09-04',
days: 1
); // -> CkDay 2026-09-07T00:00:00Z (Friday to Monday)
CkDate::AddWeekdays(
input: '2026-09-07',
days: -1
); // -> CkDay 2026-09-04T00:00:00Z
$holidays = ['2026-12-25', '2027-01-01'];
CkDate::AddWeekdays(
input: '2026-12-23',
days: 5,
holidays: $holidays
); // -> CkDay 2026-12-31T00:00:00Z
AddHours
Hours of elapsed time — a moment.
static function AddHours(
mixed $input = null,
int $hours = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
hours |
int |
0 |
Hours to add; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A moment to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, in UTC, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The moment reached, a Carbon in UTC | The moment |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Elapsed time: 24 hours is always 24 hours, whatever any zone's clocks did in between, and
no zone comes into it. Read the way ToCarbon reads a value and handed out on
its clock. "The same time tomorrow" on a wall clock is a different thing — 23 or 25 hours
away across a daylight-saving change — and there is no moment method for it here: ask the
Carbon InZone hands out, whose ->addDay() keeps the wall-clock time.
use SafeCoder\CommonKit\CkDate;
CkDate::AddHours(
input: '2026-08-31T14:05:00Z',
hours: 24
); // -> Carbon 2026-09-01T14:05:00Z
// 9:00 in Chicago on 7 March; the clocks go forward that night
CkDate::AddHours(
input: '2026-03-07T15:00:00Z',
hours: 24
); // -> Carbon 2026-03-08T15:00:00Z (hours are hours: 10:00 in Chicago)
CkDate::InZone(
input: '2026-03-07T15:00:00Z',
zone: 'America/Chicago'
)->addDay(); // -> Carbon 2026-03-08T14:00:00Z (the Carbon keeps the wall clock: 9:00 in Chicago)
AddMinutes
Minutes of elapsed time — a moment.
static function AddMinutes(
mixed $input = null,
int $minutes = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
minutes |
int |
0 |
Minutes to add; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A moment to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, in UTC, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The moment reached, a Carbon in UTC | The moment |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
A session that ends in 30, a code that expires in 10.
use SafeCoder\CommonKit\CkDate;
CkDate::AddMinutes(
input: '2026-08-31T14:05:00Z',
minutes: 30
); // -> Carbon 2026-08-31T14:35:00Z
AddSeconds
Seconds of elapsed time — a moment.
static function AddSeconds(
mixed $input = null,
int $seconds = 0,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
seconds |
int |
0 |
Seconds to add; negative to go back. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A moment to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, in UTC, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The moment reached, a Carbon in UTC | The moment |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
The expires_in of a token, added to the moment it was issued.
use SafeCoder\CommonKit\CkDate;
$issuedAt = '2026-08-31T14:05:00Z';
CkDate::AddSeconds(
input: $issuedAt,
seconds: 3600
); // -> Carbon 2026-08-31T15:05:00Z (an hour later)
MonthsBetween
Whole months from one date to another.
static function MonthsBetween(
mixed $from = null,
mixed $to = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
int $fallback = -2147483648,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
from |
mixed |
null |
The earlier date, or the later one for a negative answer. |
to |
mixed |
null |
The date to count to. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
-2147483648 |
What comes back when a date cannot be read — the lowest 32-bit integer, a number no two dates are apart. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says which date cannot be used and why, instead of answering the fallback. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both dates read | The signed number of whole months | The count |
| Rejected: either date unreadable | The fallback |
Throws CkDateException — carries Input, Expected and Reason |
The time of day is ignored. A month is complete when the day of the month comes round again — the rule Age counts years by — so from 31 January, 28 February is still 0 and 1 March is 1. That differs on purpose from AddMonths, which has to land somewhere and picks the 28th.
use SafeCoder\CommonKit\CkDate;
CkDate::MonthsBetween(
from: '2026-01-15',
to: '2026-08-14'
); // -> 6
CkDate::MonthsBetween(
from: '2026-01-15',
to: '2026-08-15'
); // -> 7
CkDate::MonthsBetween(
from: '2026-08-15',
to: '2026-01-15'
); // -> -7
CkDate::MonthsBetween(
from: '2026-01-31',
to: '2026-02-28'
); // -> 0
WeekdaysBetween
Business days from one date to another.
static function WeekdaysBetween(
mixed $from = null,
mixed $to = null,
mixed $holidays = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
int $fallback = -2147483648,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
from |
mixed |
null |
The earlier date, or the later one for a negative answer. |
to |
mixed |
null |
The date to count to; it is not counted itself. |
holidays |
mixed |
null |
Days that do not count: one date, or an array of them, each read like the two dates. null for none. One that cannot be read rejects the call; at most CkSettings::MaxHolidays (5,000). |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
-2147483648 |
What comes back when a date cannot be read — the lowest 32-bit integer, a number no two dates are apart. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says which date cannot be used and why, instead of answering the fallback. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both dates read | The signed number of weekdays, the first date counted and the second not | The count |
| Rejected: either date unreadable, or a holiday that cannot be read | The fallback |
Throws CkDateException — carries Input, Expected and Reason |
DaysBetween with the weekends left out, and with them the days you name
in holidays. Monday to Friday is 4, as DaysBetween has it; Friday to the Monday after
is 1, the Friday.
use SafeCoder\CommonKit\CkDate;
CkDate::WeekdaysBetween(
from: '2026-09-04',
to: '2026-09-07'
); // -> 1
CkDate::WeekdaysBetween(
from: '2026-09-07',
to: '2026-09-14'
); // -> 5
CkDate::WeekdaysBetween(
from: '2026-09-04',
to: '2026-09-09',
holidays: '2026-09-07'
); // -> 2
StartOfDay
Where a day starts, as a UTC moment — in the zone you name.
static function StartOfDay(
mixed $input = null,
?string $zone = CkSettings::DefaultZone,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose day it is, as an IANA or Windows id. null for the UTC day. An id the library does not know counts as UTC. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A moment to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, in UTC, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The first moment of its day, a Carbon in UTC | The moment |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
"Today's orders" for a customer in Chicago are not the orders since UTC midnight. Their day starts at 05:00 UTC in summer and 06:00 in winter, and this is the method that knows. Where a zone's clocks skip midnight, the day starts at the first moment that exists. A moment, not a day: for the calendar day itself, ToCarbonDay.
use SafeCoder\CommonKit\CkDate;
CkDate::StartOfDay('2026-08-31T14:05:00Z'); // -> Carbon 2026-08-31T00:00:00Z
CkDate::StartOfDay(
input: '2026-08-31T14:05:00Z',
zone: 'America/Chicago'
); // -> Carbon 2026-08-31T05:00:00Z
StartOfNextDay
Where a day ends: the first moment of the next one.
static function StartOfNextDay(
mixed $input = null,
?string $zone = CkSettings::DefaultZone,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose day it is, as an IANA or Windows id. null for the UTC day. An id the library does not know counts as UTC. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A moment to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, in UTC, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The first moment of the day after, a Carbon in UTC | The moment |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
The exclusive upper edge of a day. There is deliberately no "end of day": 23:59:59
leaves the last second out, and 23:59:59.999 is rounded up into the next day by an SQL
Server datetime column. A range closed below and open above has neither problem:
use SafeCoder\CommonKit\CkDate;
$now = '2026-08-31T14:05:00Z';
$from = CkDate::StartOfDay(
input: $now,
zone: 'America/Chicago'
); // -> Carbon 2026-08-31T05:00:00Z
$to = CkDate::StartOfNextDay(
input: $now,
zone: 'America/Chicago'
); // -> Carbon 2026-09-01T05:00:00Z
// WHERE created >= :from AND created < :to — never <=
StartOfMonth
The first of the month — a day.
static function StartOfMonth(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be read. A placeholder fallback is no fallback. It comes back as given, not moved to the edge of its month. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | That day of its month, a CkDay |
The day |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Read the way ToCarbonDay reads a value. For statement periods, "this month's orders" and the ends of a range — the off-by-one, answered once.
use SafeCoder\CommonKit\CkDate;
CkDate::StartOfMonth('8/31/2026'); // -> CkDay 2026-08-01T00:00:00Z
EndOfMonth
The last day of the month, whatever it is — a day.
static function EndOfMonth(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be read. A placeholder fallback is no fallback. It comes back as given, not moved to the edge of its month. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | That day of its month, a CkDay |
The day |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Read the way ToCarbonDay reads a value. For statement periods, "this month's orders" and the ends of a range — the off-by-one, answered once.
use SafeCoder\CommonKit\CkDate;
CkDate::EndOfMonth('2/10/2024'); // -> CkDay 2024-02-29T00:00:00Z (a leap year)
CkDate::EndOfMonth('2/10/2026'); // -> CkDay 2026-02-28T00:00:00Z
StartOfWeek
The first day of the week — a day.
static function StartOfWeek(
mixed $input = null,
DayOfWeek $firstDay = DayOfWeek::Monday,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
firstDay |
DayOfWeek |
DayOfWeek::Monday |
The day a week starts on. Monday, as ISO 8601 has it; pass DayOfWeek::Sunday for the US habit. Never read from the machine's locale. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The first day of its week, a CkDay |
The day |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
The week starts on firstDay — said by you, never taken from the machine's locale, which would give one answer on a developer's laptop and another on the server.
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\Enums\DayOfWeek;
CkDate::StartOfWeek('2026-09-02'); // -> CkDay 2026-08-31T00:00:00Z (a Wednesday; the Monday before)
CkDate::StartOfWeek(
input: '2026-09-02',
firstDay: DayOfWeek::Sunday
); // -> CkDay 2026-08-30T00:00:00Z
EndOfWeek
The last day of the week — a day.
static function EndOfWeek(
mixed $input = null,
DayOfWeek $firstDay = DayOfWeek::Monday,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
firstDay |
DayOfWeek |
DayOfWeek::Monday |
The day a week starts on. Monday, as ISO 8601 has it; pass DayOfWeek::Sunday for the US habit. Never read from the machine's locale. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The last day of its week, a CkDay |
The day |
| Rejected: unreadable, or the result is outside the calendar (years 1 to 9999), and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
The day before the next firstDay.
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\Enums\DayOfWeek;
CkDate::EndOfWeek('2026-09-02'); // -> CkDay 2026-09-06T00:00:00Z
CkDate::EndOfWeek(
input: '2026-09-02',
firstDay: DayOfWeek::Sunday
); // -> CkDay 2026-09-05T00:00:00Z
StartOfQuarter
The first day of the quarter — a day.
static function StartOfQuarter(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The first day of its quarter, a CkDay |
The day |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Calendar quarters, as Quarter counts them.
use SafeCoder\CommonKit\CkDate;
CkDate::StartOfQuarter('2026-08-10'); // -> CkDay 2026-07-01T00:00:00Z
EndOfQuarter
The last day of the quarter — a day.
static function EndOfQuarter(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The last day of its quarter, a CkDay |
The day |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
Calendar quarters, as Quarter counts them.
use SafeCoder\CommonKit\CkDate;
CkDate::EndOfQuarter('2026-08-10'); // -> CkDay 2026-09-30T00:00:00Z
StartOfYear
The first of January — a day.
static function StartOfYear(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The first day of its year, a CkDay |
The day |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
The lower edge of a yearly report.
use SafeCoder\CommonKit\CkDate;
CkDate::StartOfYear('2026-08-10'); // -> CkDay 2026-01-01T00:00:00Z
EndOfYear
The thirty-first of December — a day.
static function EndOfYear(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A date to use when the value cannot be used. A placeholder fallback is no fallback. It comes back as given, never moved. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when neither the value nor the fallback can be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The last day of its year, a CkDay |
The day |
| Rejected: unreadable, and no usable fallback | Year one, the sentinel: a plain Carbon at 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason |
The upper edge of a yearly report.
use SafeCoder\CommonKit\CkDate;
CkDate::EndOfYear('2026-08-10'); // -> CkDay 2026-12-31T00:00:00Z
Quarter
Which quarter of the year.
static function Quarter(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
int $fallback = 0,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
0 |
What comes back when the value cannot be read — no quarter is 0. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the fallback, when the value cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | 1 to 4, January to March being 1 | The quarter |
| Rejected: unreadable | The fallback |
Throws CkDateException — carries Input, Expected and Reason |
Calendar quarters. A fiscal year that starts elsewhere is the application's arithmetic on top of this.
use SafeCoder\CommonKit\CkDate;
CkDate::Quarter('2026-08-31'); // -> 3
CkDate::Quarter('junk'); // -> 0
WeekOfYear
The ISO 8601 week number.
static function WeekOfYear(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
?string $zone = CkSettings::DefaultZone,
bool $treatSentinelsAsMissing = true,
int $fallback = 0,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): int
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone whose calendar day counts, as an IANA or Windows id; null for the UTC day. Only a value with a time of day is placed — "today" included: a date alone is that day everywhere, and no zone moves it. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
int |
0 |
What comes back when the value cannot be read — no week is 0. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the fallback, when the value cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | 1 to 53 | The week |
| Rejected: unreadable | The fallback |
Throws CkDateException — carries Input, Expected and Reason |
Weeks start on Monday, and week 1 is the one holding the year's first Thursday. The days around New Year can therefore belong to the other year's weeks, which is where a hand-written week number goes wrong.
use SafeCoder\CommonKit\CkDate;
CkDate::WeekOfYear('2026-08-31'); // -> 36
CkDate::WeekOfYear('2027-01-01'); // -> 53 (a Friday: still the last week of 2026)
CkDate::WeekOfYear('2025-12-29'); // -> 1 (a Monday: already the first week of 2026)
InZone
A moment on a zone's wall clock.
static function InZone(
mixed $input = null,
?string $zone = CkSettings::DefaultZone,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
?\DateTimeInterface $fallback = null,
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): Carbon
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
zone |
?string |
CkSettings::DefaultZone (null) |
The time zone to show the moment in — an IANA id like 'Europe/Berlin' or a Windows id like 'W. Europe Standard Time'. An id the library does not know counts as UTC; a blank one is rejected. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
?\DateTimeInterface |
null |
A moment to use when the value cannot be read. A placeholder fallback is no fallback. It comes back as given. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the year-one sentinel, when the value, the fallback or the zone cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| The value reads | The moment as a Carbon carrying the zone: ->format('Y-m-d H:i') writes the wall clock, ->getTimestamp() is unchanged |
The moment |
| Rejected: an unreadable value with no usable fallback, or a blank zone | Year one, the sentinel: 0001-01-01T00:00:00Z |
Throws CkDateException — carries Input, Expected and Reason — Expected says "a time zone" or "a date" |
The reverse of what assumeZone does on the way in: the value is read into UTC first, as
ToCarbon reads it, then shown on the zone's clock. The Carbon keeps the moment
and carries the zone, so its own methods answer on that clock — ->format() writes the
wall clock, ->addDay() keeps the wall-clock time across a daylight-saving change — and
Format writes the digits it shows. For emails, reports and anything a person
reads in their own time.
use SafeCoder\CommonKit\CkDate;
CkDate::InZone(
input: '2026-08-31T14:05:00Z',
zone: 'America/Chicago'
)->format('Y-m-d H:i'); // -> '2026-08-31 09:05'
CkDate::InZone(
input: '2026-08-31T14:05:00Z',
zone: 'Europe/Berlin'
)->format('H:i'); // -> '16:05'
CkDate::InZone(
input: '2026-08-31T14:05:00Z',
zone: 'Central Standard Time'
)->tzName; // -> 'America/Chicago' (a Windows id is understood)
CkDate::InZone(
input: '2026-08-31T14:05:00Z',
zone: 'America/Chicago'
); // -> Carbon 2026-08-31T14:05:00Z (the same moment, on Chicago's clock)
TimeAgo
A moment as people say it.
static function TimeAgo(
mixed $input = null,
mixed $asOf = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true,
string $fallback = '',
bool $throwIfInvalid = CkSettings::DefaultThrowIfInvalid
): string
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The moment: a date object, a number, or text. |
asOf |
mixed |
null |
The moment to measure from. null means now (UTC) — pass a value in tests. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y H:i'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. null means zone-less digits count as UTC. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
fallback |
string |
'' |
What comes back when a moment cannot be read. |
throwIfInvalid |
bool |
CkSettings::DefaultThrowIfInvalid (false) |
Pass true to throw a CkDateException that says why, instead of answering the fallback, when a moment cannot be used. |
Returns
| When | Default | With throwIfInvalid: true |
|---|---|---|
| Both moments read | "just now", "5 minutes ago", "an hour ago", "yesterday", "3 weeks ago", "in 2 days", "a year ago"… | The phrase |
| Rejected: either cannot be read | The fallback |
Throws CkDateException — carries Input, Expected and Reason |
English, and deliberately coarse: under 45 seconds is "just now", then minutes, hours,
"yesterday" or "tomorrow", days to a week, weeks to a month, months to a year, then
years, each rounded to the nearest whole, halves up. For activity feeds and "last seen"
labels; an exact timestamp belongs beside it, not instead of it. For a phrase in the
reader's language, ask the Carbon ToCarbon hands out:
->locale('ro')->diffForHumans().
use SafeCoder\CommonKit\CkDate;
CkDate::TimeAgo(
input: '2026-08-28T12:00:00Z',
asOf: '2026-08-31T12:00:00Z'
); // -> '3 days ago'
CkDate::TimeAgo(
input: '2026-08-31T10:30:00Z',
asOf: '2026-08-31T12:00:00Z'
); // -> '2 hours ago' (90 minutes, halves up)
CkDate::TimeAgo(
input: '2026-08-30T12:00:00Z',
asOf: '2026-08-31T12:00:00Z'
); // -> 'yesterday'
CkDate::TimeAgo(time() + 7200); // -> 'in 2 hours'
Explain
Why not — in words, without an exception.
static function Explain(
mixed $input = null,
DateOrder $order = CkSettings::DefaultDateOrder,
?string $format = null,
?string $assumeZone = CkSettings::DefaultAssumeZone,
bool $treatSentinelsAsMissing = true
): string
| Parameter | Type | Default | What it does |
|---|---|---|---|
input |
mixed |
null |
The value: a date object, a number, or text. |
order |
DateOrder |
CkSettings::DefaultDateOrder (DateOrder::MonthFirst) |
How an ambiguous all-numeric date like '3/4/2026' reads. |
format |
?string |
null |
An exact PHP date format, e.g. 'd.m.Y'; when given, text must match it and nothing else is tried. |
assumeZone |
?string |
CkSettings::DefaultAssumeZone (null) |
The time zone the value's digits are wall-clock in, when the value declares no zone of its own. A date alone is never moved by it, as everywhere in this class. An id the library does not know counts as UTC. |
treatSentinelsAsMissing |
bool |
true |
Counts the placeholder dates as missing rather than real. |
Returns
| When | You get |
|---|---|
| The value passes the reader | '' |
| It does not | The reason, the same words the exception would carry — an answer, not a failure; it never throws |
The form-friendly sibling of throwIfInvalid: the message to show beside the field,
without a try and catch. Every class with a reader has one, on the reader's own
rules, so the reason is exactly what the reader would say — and it is empty exactly when
IsDate is true.
use SafeCoder\CommonKit\CkDate;
CkDate::Explain('8/31/2026'); // -> ''
CkDate::Explain('5/6/26'); // -> 'a two-digit year is ambiguous'
CkDate::Explain('31/12/2026'); // -> 'not a date in month-first order'
CkDate::Explain('1900-01-01'); // -> 'a placeholder date that means missing (1900-01-01)'
CkDate::Explain('junk'); // -> 'not a recognisable date'
CkDate::Explain(null); // -> 'no value'
Good to know
- Days and moments, one class. Every method says which it answers: a
CkDay— a Carbon at midnight UTC that says it is a day — fromToCarbonDay, theAddfamily and the edges of a week, a month, a quarter and a year; a Carbon in UTC fromToCarbon,AddHours,AddMinutes,AddSeconds,StartOfDayandStartOfNextDay.IsPast,IsFuture,IsToday,IsBetween,IsWeekdayandIsWeekendcompare calendar days — a value dated today is neither past nor future whatever its time of day;IsBefore,IsAfterandSameDaycompare moments — 09:00 is before 10:00 on the same day, and equal moments are neither. - A
CkDayis a date alone. Read again by the library it is that day on every clock, never moved byzoneorassumeZone, with no time of day — where a plain date object at midnight UTC is a moment, the evening before west of Greenwich.IsDateAlone()says whether it still is one. A copy made by another date class, or the text JSON makes of it, is a plain moment again: hand a day on as->format('Y-m-d'). The caller'sfallbackand the year-one sentinel come back as plain Carbons. - The day of a moment is the UTC day — unless the
zoneparameter, orCkSettings::DefaultZonebehind it, names whose day is wanted. A date alone is never moved, by that zone or byassumeZone:'2026-08-31'is 31 August on every clock. - Set your zone once.
CkSettings::DefaultZonestands behind everyzoneparameter andCkSettings::DefaultAssumeZonebehind everyassumeZone— see Your time zone, set once, and read its warning before setting the second. Set, the second is also the clock every moment is handed out on: a Carbon carrying that zone instead of UTC,Formatwriting that clock, the day methods unchanged. - Two zone parameters, two questions.
assumeZonesays which wall clock a zone-less value was written on.zonesays whose calendar the answer is counted on — inToCarbonDay,IsToday,StartOfDay,SameDay. A value that declares its own zone always wins overassumeZone. An id the library does not know counts as UTC in either — a zone is a preference about a value that is readable on its own, never a reason to refuse it; both IANA and Windows ids are understood. - Calendar units and clock units are different things.
Add,AddDays,AddMonths,AddYearsandAddWeekdaysmove by the calendar and answer a day.AddHours,AddMinutesandAddSecondsare elapsed time and answer a moment. A day is 23, 24 or 25 hours long, which is the whole difference; for the same wall-clock time a day later, ask the CarbonInZonehands out. - Daylight saving, once and for all. Reading a wall-clock time that never existed — the spring-forward hour — is rejected: someone wrote down a time that was not on the clock. The repeated fall-back hour reads as standard time.
- A day is a range, closed below and open above.
StartOfDayandStartOfNextDay; compare with>=and<. There is no "end of day" to get wrong. - Month-first is the default order.
'3/4/2026'reads as March 4th unless you passorder: DateOrder::DayFirst. An impossible date under the declared order —'13/5/2026'month-first — is rejected, never silently re-read the other way. Declare the order once per data source and stay honest. - Two-digit years are rejected.
'5/6/26'answers the year-one sentinel rather than being assigned a century by guess. - Placeholder dates count as missing by default. 0001-01-01, 1899-12-30, 1900-01-01,
2079-06-06 and 9999-12-31 are the dates databases use for "nobody entered anything".
Pass
treatSentinelsAsMissing: falsewhen one of them is real data. A value with no zone is judged by its digits as written, beforeassumeZonemoves them — so'1900-01-01 00:00'is the placeholder on every clock — and a value that declares its zone on its UTC day. - Zone-less digits count as UTC. A value with no zone of its own is declared UTC,
never shifted — unless you name the wall clock it was written in with
assumeZone. A value that declares its own zone always wins. - Fallbacks are checked too. A
fallbackgoes through the same reading; a placeholder fallback is no fallback, so a bad default cannot slip through. - Formats are PHP's.
formatreads andintowrites a PHP date format —'d.m.Y','Y-m-d H:i:s'— the onedate(),DateTimeand Carbon use;Iso8601is the one fixed shape. A reading format is strict where PHP's own parser is lenient. - There is no time-of-day reader. PHP has no type for a time of day alone.
IsTimechecks one,HasTimesays whether a value carries one at all, andFormatwithinto: 'H:i:s'writes one. IsIso8601judges the shape,IsDatethe meaning.'8/31/2026'is a date but not ISO text;'1900-01-01'is ISO text but, being a placeholder, not a date by default.- A fraction of a second keeps six digits, as PHP's date objects do;
Unixfloors them. - Today is today in the
zone— UTC when none is set.Age,IsAdult,IsPast,IsFuture,IsTodayandTimeAgomeasure against this moment when you give noasOf; at one in the morning in Bucharest it is still yesterday in UTC, which is whatzoneis for. PassasOfin tests:Carbon::setTestNow()on your application's Carbon does not reach the library's own copy. - Holidays are yours.
AddWeekdaysandWeekdaysBetweenknow Saturdays and Sundays and nothing else: no country's holidays are written into the library. - The Carbon is the library's own copy.
SafeCoder\CommonKit\Packages\Carbon\Carbon, not the Carbon class your application or framework ships, so a method that type-hints the one does not accept the other. Both are PHPDateTimeobjects: anything typed\DateTimeInterfacetakes either, each takes the other as an argument, andCarbon::instance($value)on your own class converts in one call. - Reader versus sentinel.
ToCarbonOrNullhands back a new Carbon in UTC; CkSentinel::ToNullnever touches the value — the same object, in the zone it came in — it only nulls the placeholders. Use the reader when parsing, the sentinel in save hooks.
If you want errors instead
Pass throwIfInvalid: true and a value that cannot be used raises a CkDateException
(from SafeCoder\CommonKit\Exceptions) instead of a sentinel, empty text or a fallback.
The exception carries the Input, what was Expected — "a moment in time" from the
moment methods, "a date" from the day methods — and a short Reason, flattened into a
one-line message. The OrNull twins have no such switch — they never throw; null
is their answer for anything unusable. When a bad date should be an error, read it with
the plain sibling and throwIfInvalid: true.
use SafeCoder\CommonKit\CkDate;
CkDate::ToCarbon(
input: 'junk',
throwIfInvalid: true
); // throws CkDateException (the message says why)
CkDate::ToCarbonDay(
input: 'junk',
throwIfInvalid: true
); // throws CkDateException (Expected says "a date")
CkDate::ToCarbonOrNull('junk'); // -> null (the OrNull siblings never throw)
CkDate::ToCarbonDayOrNull('junk'); // -> null