Skip to content

fix(I18n): compute correct date in Time::today(), yesterday(), and tomorrow() across timezones - #10532

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/time-today-timezone
Open

fix(I18n): compute correct date in Time::today(), yesterday(), and tomorrow() across timezones#10532
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/time-today-timezone

Conversation

@gr8man

@gr8man gr8man commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Description
Time::today($timezone), Time::yesterday($timezone), and Time::tomorrow($timezone) previously used PHP's native date('Y-m-d 00:00:00') and strtotime('-1 day') / strtotime('+1 day') to generate the date string passed to the constructor.

Because date() and strtotime() evaluate in the server's default timezone (date_default_timezone_get()), the target $timezone argument was ignored when determining the calendar day. For several hours each day (when the server timezone like UTC and the requested timezone like Asia/Tokyo or America/New_York are on different calendar days), today(), yesterday(), and tomorrow() produced the wrong calendar day in that timezone.

Furthermore, passing a non-empty string from date() into new static(...) bypassed Time::setTestNow() in the constructor, preventing today(), yesterday(), and tomorrow() from respecting mocked test time.

This PR fixes both issues by using static::now($timezone, $locale)->setTime(0, 0, 0, 0) (and modify('-1 day') / modify('+1 day')). This ensures:

  1. Midnight is computed relative to the requested timezone.
  2. Time::setTestNow() mock instances are preserved and converted accurately.
  3. Unit tests cover date boundary transitions across different timezones.
  4. A changelog entry is added to v4.7.5.rst.

Checklist:

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

…terday(), and tomorrow()

- Compute start of day relative to the requested timezone instead of the server default timezone
- Respect Time::setTestNow() mock instance in today(), yesterday(), and tomorrow()
- Add unit tests covering date boundary transitions across different timezones
- Add changelog entry in v4.7.5.rst
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.

1 participant