Skip to content

fix(flags): stop DateInterval rolling relative dates into the next month - #315

Merged
marandaneto merged 2 commits into
PostHog:mainfrom
luantaraschi:fix/relative-date-month-clamp
Nov 11, 2026
Merged

marandaneto merged 2 commits into
PostHog:mainfrom
luantaraschi:fix/relative-date-month-clamp

Conversation

@luantaraschi

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

relativeDateParseForFeatureFlagMatching subtracts months and years with DateInterval, and PHP normalizes an impossible date forward. 1m on March 31 lands on March 3, 1m on May 31 on May 1, and 1y on February 29 on March 1. The local evaluator spec subtracts one unit at a time and clamps after every step, so those should be February 28, April 30 and February 28, and 2m on March 31 should be January 28 (the spec's own scenario). The same fix was merged for posthog-js (PostHog/posthog-js#5626), posthog-go (PostHog/posthog-go#426) and posthog-python (PostHog/posthog-python#1212).

For a flag with an is_date_after 1m condition evaluated locally on March 31, a person whose date was March 1 or March 2 didn't match.

The parser still works in UTC where the spec asks for the team's timezone. I left that alone.

💚 How did you test it?

  • 12 new data provider cases in FeatureFlagLocalEvaluationTest. Nine of them fail on main.
  • I compared the parser with a month-by-month loop for every day from 2019 through 2028, with lookbacks of 1 to 25 months and 1 to 5 years: 109,590 cases. main differs in 5,803 of them, 1m included on 67 days. With this change there are no differences.
  • I evaluated a flag locally through Client, with the clock frozen at 2025-03-31 and an is_date_after 1m condition. A last_purchase of March 1 or March 2 now matches, and February 27 still doesn't.
  • In Docker: the full PHPUnit suite on PHP 8.2, 8.3, 8.4 and 8.5 (2704 tests; the single PHPUnit warning shows up on main too), phpcs with no new warnings, composer api:check, the adapter tests, and scripts/test-laravel.sh for Laravel 12 and 13.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Added a change intent for posthog-php (written by hand, I didn't run pnpm change)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Luan Taraschi directed the work and reviewed the diff. Claude Code (Claude Opus 5.5) wrote the change and ran the checks. DateInterval is replaced for months and years with two small helpers built on setDate. The month loop stops once the day is 28 or less, so it runs at most 11 times whatever the lookback.

DateInterval normalizes an impossible date forward, so 1m on March 31 landed on March 3 and 1y on February 29 on March 1. Step back one unit at a time and clamp the day after each step, as the SDK spec asks.

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks good; no qualifying findings. Verdict: correct.

@marandaneto
marandaneto enabled auto-merge (squash) November 11, 2026 11:41
@marandaneto
marandaneto merged commit 17b1da5 into PostHog:main Nov 11, 2026
30 checks passed
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.

2 participants