Skip to content

feat: add Time::addCalendarMonths() and Time::subCalendarMonths() methods - #9528

Merged
paulbalandan merged 12 commits into
codeigniter4:4.7from
christianberkman:4.7-addCalendarMonth
Apr 25, 2025
Merged

feat: add Time::addCalendarMonths() and Time::subCalendarMonths() methods#9528
paulbalandan merged 12 commits into
codeigniter4:4.7from
christianberkman:4.7-addCalendarMonth

Conversation

@christianberkman

@christianberkmanchristianberkman commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

The Time::addMonths() could return an undesired behaviour if a date in the next calandar month is desired. For example, Time(2025-01-31)->addMonths(1) results in 2025-03-03 while a date in february may be desired, or Time(2025-03-31)->addMonths(1) results in a date in May not in April.

The addCalendarMonths function returns a new date in the next calendar month.
New: Also implemented subCalendarMonths function to return a new date in a previous calendar month.

Of course, this only applies if the dev is looking for a date in the next calendar month (months are weird of course, it is 30, 31, 28, 29 days...? who knows).

If deemed worthy to include in 4.7 I will write test and documentation.

Comparison between addMonths() and addCalendarMonths()

Initial date: 2025-01-31
+---------+------------+-------------------+
| $months | addMonths | addCalendarMonths |
+---------+------------+-------------------+
| 0 | 2025-01-31 | 2025-01-31 |
| 1 | 2025-03-03 | 2025-02-28 |
| 2 | 2025-03-31 | 2025-03-31 |
| 3 | 2025-05-01 | 2025-04-30 |
| 4 | 2025-05-31 | 2025-05-31 |
| 5 | 2025-07-01 | 2025-06-30 |
| 6 | 2025-07-31 | 2025-07-31 |
| 7 | 2025-08-31 | 2025-08-31 |
| 8 | 2025-10-01 | 2025-09-30 |
| 9 | 2025-10-31 | 2025-10-31 |
| 10 | 2025-12-01 | 2025-11-30 |
| 11 | 2025-12-31 | 2025-12-31 |
| 12 | 2026-01-31 | 2026-01-31 |
| 13 | 2026-03-03 | 2026-02-28 |
+---------+------------+-------------------+

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@michalsn

Copy link
Copy Markdown
Member

Sounds useful.

In addition to fixing PHPStan, we also need:

  • tests for this new method
  • updated user guide
  • changelog entry

@michalsnmichalsn added enhancement PRs that improve existing functionalities tests needed Pull requests that need tests 4.7 docs needed Pull requests needing documentation write-ups and/or revisions. labels Apr 18, 2025
@christianberkman

christianberkman commented Apr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Happy to write tests/docs/changelog.

I had tried returntype Time brefore but this also returns an error. Functions like addMonth() do not have any return type specified. What is the correct type?

Is there a way to run PHPStan only on one file, or does this break the testing? composer phpstan:check system/I18n/TimeTrait.php does not return any errors.

@paulbalandan

Copy link
Copy Markdown
Member

For phpstan, you should run it on the whole codebase, not just the changed file.

For testing, you can just add your example as a data provider then test on them.

Initial date: 2025-01-31
+---------+------------+-------------------+
| $months | addMonths | addCalendarMonths |
+---------+------------+-------------------+
| 0 | 2025-01-31 | 2025-01-31 |
| 1 | 2025-03-03 | 2025-02-28 |
| 2 | 2025-03-31 | 2025-03-31 |
| 3 | 2025-05-01 | 2025-04-30 |
| 4 | 2025-05-31 | 2025-05-31 |
| 5 | 2025-07-01 | 2025-06-30 |
| 6 | 2025-07-31 | 2025-07-31 |
| 7 | 2025-08-31 | 2025-08-31 |
| 8 | 2025-10-01 | 2025-09-30 |
| 9 | 2025-10-31 | 2025-10-31 |
| 10 | 2025-12-01 | 2025-11-30 |
| 11 | 2025-12-31 | 2025-12-31 |
| 12 | 2026-01-31 | 2026-01-31 |
| 13 | 2026-03-03 | 2026-02-28 |
+---------+------------+-------------------+

@christianberkman

christianberkman commented Apr 21, 2025

Copy link
Copy Markdown
ContributorAuthor

For phpstan, you should run it on the whole codebase, not just the changed file.

For testing, you can just add your example as a data provider then test on them.

Thank you, I figured out I need to run on the whole code base. Error should be gone now.

I wrote tests similar to testCanAddMonths() etc. I used a date that with a result in February.

Comment threadsystem/I18n/TimeTrait.php
Comment threadsystem/I18n/TimeTrait.php
Comment threaduser_guide_src/source/changelogs/v4.7.0.rst Outdated
christianberkmanand others added 3 commits April 21, 2025 17:17
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
revert change to TimeTrait::setTimeNow()
Comment threaduser_guide_src/source/libraries/time/031.php
@paulbalandanpaulbalandan removed the tests needed Pull requests that need tests label Apr 21, 2025
Comment threaduser_guide_src/source/libraries/time.rst Outdated
Co-authored-by: Michal Sniatala <michal@sniatala.pl>
@michalsnmichalsn removed the docs needed Pull requests needing documentation write-ups and/or revisions. label Apr 23, 2025
@paulbalandan
paulbalandan merged commit 9d12f04 into codeigniter4:4.7Apr 25, 2025
@paulbalandan

Copy link
Copy Markdown
Member

Thank you, @christianberkman 🎉

@paulbalandanpaulbalandan changed the title feat: add Time::addCalendarMonths() functionfeat: add Time::addCalendarMonths() and Time::subCalendarMonths() methodsApr 25, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementPRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@christianberkman@michalsn@paulbalandan