Skip to content

feat: add Date::earliestOf and Date::latestOf helper functions#960

Merged
Martin Georgiev (martin-georgiev) merged 3 commits into
masterfrom
date-min-and-max
Jun 11, 2025
Merged

feat: add Date::earliestOf and Date::latestOf helper functions#960
Martin Georgiev (martin-georgiev) merged 3 commits into
masterfrom
date-min-and-max

Conversation

@jan-j

@jan-j Jan Jablonski (jan-j) commented Jun 10, 2025

Copy link
Copy Markdown
Contributor

I often find it necessary to compare two or more Date instances in order to establish which one of them is the earliest or the latest.

My proposal here is to add two static methods Date::max and Date::min that mimic PHP's native max and min function.

There's also an alternative naming to consider latest and earliest, but since we already use native max and min with DateTime I picked these ones.

I also considered adding these as non-static methods that would be used like

$dateA = Date::fromYearMonthDayString('2018-01-01');
$dateB = Date::fromYearMonthDayString('2018-01-02');
$dateC = Date::fromYearMonthDayString('2018-01-03');

$max = $dateA->max($dateB, $dateC);

but I think it feels a little wrong and calling it this way

$max = Date::max($dateA, $dateB, $dateC);

seems better.

Update

Methods were renamed to Date::earliestOf and Date::latestOf.

@jan-j
Jan Jablonski (jan-j) requested review from a team as code owners June 10, 2025 11:17
@martin-georgiev

Copy link
Copy Markdown
Contributor

Wdyt about making this a bit more semantic with a rename to latestOf(Date ...$datesToCompare) and earliestOf(Date ...$datesToCompare)?

Comment thread lib/Date.php Outdated
/**
* @return self The earliest date from the provided dates.
*/
public static function min(self $date, self ...$otherDates): self

@martin-georgiev Martin Georgiev (martin-georgiev) Jun 10, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static function min(self $date, self ...$otherDates): self
public static function min(self $initialCutOffDate, self ...$otherDates): self

So it is clear that the intention here is to use the date in a particular way.

@jan-j

Copy link
Copy Markdown
Contributor Author

Martin Georgiev (@martin-georgiev)

I like the latestOf and earliestOf, happy to rename them like this.

Not sure if the initialCutOffDate isn't a little more confusing though. But I'm not too fussed really.

I was wondering if these methods should allow empty list of dates or not and ruled it out because it would make the return value nullable which is often a pain in the ass to handle on the user side.
Alternative to defining an initialCutOffDate first parameter is to do what you suggested as in latestOf(Date ...$datesToCompare) and then enforcing that $datesToCompare is a non-empty list via PHPStan and/or exception on empty list.

@jan-j Jan Jablonski (jan-j) changed the title feat: add Date::max and Date::min helper functions feat: add Date::earliestOf and Date::latestOf helper functions Jun 10, 2025
Comment thread lib/Date.php Outdated
Comment thread lib/Date.php Outdated
/**
* @return self The earliest date from the provided dates.
*/
public static function earliestOf(self ...$datesToCompare): self

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static function earliestOf(self ...$datesToCompare): self
public static function earliestOf(self $date, self ...$datesToCompare): self

nit: this will require at least one argument in the fn invocation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed, had it like this before, but changed it thinking I can enforce it with PHPStan, but that seemed impossible.

@martin-georgiev

Copy link
Copy Markdown
Contributor

Nice, it reads more intuitive now and code feels self-documented. Upvote from me ;)

Merged via the queue into master with commit 9d6b286 Jun 11, 2025
9 checks passed
@martin-georgiev
Martin Georgiev (martin-georgiev) deleted the date-min-and-max branch June 11, 2025 10:05
@lendabot lendabot Bot mentioned this pull request Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants