From 2a4afc214334ac23172818354b14be7155758ca0 Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:43:40 -0300 Subject: [PATCH 1/2] fix(flags): stop DateInterval rolling relative dates into the next month 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. --- .changeset/relative-date-month-clamp.md | 5 ++++ lib/FeatureFlag.php | 37 +++++++++++++++++++++++-- test/FeatureFlagLocalEvaluationTest.php | 26 +++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 .changeset/relative-date-month-clamp.md diff --git a/.changeset/relative-date-month-clamp.md b/.changeset/relative-date-month-clamp.md new file mode 100644 index 00000000..ded917ec --- /dev/null +++ b/.changeset/relative-date-month-clamp.md @@ -0,0 +1,5 @@ +--- +"posthog-php": patch +--- + +Relative date filters in local flag evaluation no longer roll past the end of a shorter month. `1m` on March 31 now resolves to February 28 instead of March 3. diff --git a/lib/FeatureFlag.php b/lib/FeatureFlag.php index 7b36a376..5d556213 100644 --- a/lib/FeatureFlag.php +++ b/lib/FeatureFlag.php @@ -529,9 +529,9 @@ public static function relativeDateParseForFeatureFlagMatching($value) } elseif ($interval == "w") { $parsedDt->sub(new \DateInterval("P{$number}W")); } elseif ($interval == "m") { - $parsedDt->sub(new \DateInterval("P{$number}M")); + FeatureFlag::subtractMonths($parsedDt, $number); } elseif ($interval == "y") { - $parsedDt->sub(new \DateInterval("P{$number}Y")); + FeatureFlag::subtractYears($parsedDt, $number); } else { return null; } @@ -542,6 +542,39 @@ public static function relativeDateParseForFeatureFlagMatching($value) } } + /** + * Step back one month at a time and clamp the day after every step, as the SDK + * spec requires: March 31 minus two months is January 28. DateInterval would + * normalize February 31 forward into March. + */ + private static function subtractMonths(\DateTime $date, int $months): void + { + $year = (int) $date->format('Y'); + $month = (int) $date->format('n'); + $day = (int) $date->format('j'); + // Every month has at least 28 days, so nothing can clamp further after that. + for ($step = 1; $step <= $months && $day > 28; $step++) { + // Day 0 of a month is the last day of the month before it. + $lastDay = (int) (new \DateTime('@0'))->setDate($year, $month - $step + 1, 0)->format('j'); + $day = min($day, $lastDay); + } + $date->setDate($year, $month - $months, $day); + } + + /** + * Step back whole years the same way. Only February 29 can clamp, and once it + * becomes February 28 in the year before, no earlier step changes it again. + */ + private static function subtractYears(\DateTime $date, int $years): void + { + $month = (int) $date->format('n'); + $day = (int) $date->format('j'); + if ($years > 0 && $month === 2 && $day === 29) { + $day = 28; + } + $date->setDate((int) $date->format('Y') - $years, $month, $day); + } + /** * Parse a semver numeric identifier (e.g., the "1" in "1.2.3"). * diff --git a/test/FeatureFlagLocalEvaluationTest.php b/test/FeatureFlagLocalEvaluationTest.php index e24a7c7e..5276d2c1 100644 --- a/test/FeatureFlagLocalEvaluationTest.php +++ b/test/FeatureFlagLocalEvaluationTest.php @@ -1629,6 +1629,32 @@ public function testRelativeDateParsingMonths() }); } + public static function relativeDateClampProvider(): array + { + return [ + 'one month from March 31' => ['2025-03-31T12:00:00Z', '1m', '2025-02-28T12:00:00Z'], + 'one month from May 31' => ['2025-05-31T12:00:00Z', '1m', '2025-04-30T12:00:00Z'], + 'one month into a leap February' => ['2024-03-31T12:00:00Z', '1m', '2024-02-29T12:00:00Z'], + 'two months keep the February clamp' => ['2025-03-31T12:00:00Z', '2m', '2025-01-28T12:00:00Z'], + 'thirteen months' => ['2025-03-31T12:00:00Z', '13m', '2024-02-28T12:00:00Z'], + 'ten months from December 31' => ['2025-12-31T12:00:00Z', '10m', '2025-02-28T12:00:00Z'], + 'twelve months pass through February' => ['2019-01-29T12:00:00Z', '12m', '2018-01-28T12:00:00Z'], + 'one year does not' => ['2019-01-29T12:00:00Z', '1y', '2018-01-29T12:00:00Z'], + 'zero months' => ['2025-03-31T12:00:00Z', '0m', '2025-03-31T12:00:00Z'], + 'one year from February 29' => ['2024-02-29T12:00:00Z', '1y', '2023-02-28T12:00:00Z'], + 'four years from February 29' => ['2024-02-29T12:00:00Z', '4y', '2020-02-28T12:00:00Z'], + 'zero years from February 29' => ['2024-02-29T12:00:00Z', '0y', '2024-02-29T12:00:00Z'], + ]; + } + + #[DataProvider('relativeDateClampProvider')] + public function testRelativeDateParsingClampsAfterEveryStep(string $now, string $value, string $expected) + { + $this->executeAtFrozenDateTime(new \DateTime($now), function () use ($value, $expected) { + self::assertEquals(new \DateTime($expected), FeatureFlag::relativeDateParseForFeatureFlagMatching($value)); + }); + } + public function testRelativeDateParsingYears() { $this->executeAtFrozenDateTime(new \DateTime('2020-01-01T12:01:20Z'), function () { From f8cf18f67336b86c129e9fbd0a33e8a06801259a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 11 Nov 2026 12:46:34 +0100 Subject: [PATCH 2/2] style(flags): wrap long lines to satisfy CI --- lib/FeatureFlag.php | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/FeatureFlag.php b/lib/FeatureFlag.php index 5d556213..0f6b0540 100644 --- a/lib/FeatureFlag.php +++ b/lib/FeatureFlag.php @@ -140,27 +140,45 @@ private static function matchPropertyValue($property, $propertyValues, $property } if ($operator == "icontains") { - return strpos(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))) !== false; + return strpos( + strtolower(FeatureFlag::valueToString($overrideValue)), + strtolower(FeatureFlag::valueToString($value)) + ) !== false; } if ($operator == "not_icontains") { - return strpos(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))) === false; + return strpos( + strtolower(FeatureFlag::valueToString($overrideValue)), + strtolower(FeatureFlag::valueToString($value)) + ) === false; } if ($operator == "starts_with") { - return str_starts_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))); + return str_starts_with( + strtolower(FeatureFlag::valueToString($overrideValue)), + strtolower(FeatureFlag::valueToString($value)) + ); } if ($operator == "not_starts_with") { - return !str_starts_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))); + return !str_starts_with( + strtolower(FeatureFlag::valueToString($overrideValue)), + strtolower(FeatureFlag::valueToString($value)) + ); } if ($operator == "ends_with") { - return str_ends_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))); + return str_ends_with( + strtolower(FeatureFlag::valueToString($overrideValue)), + strtolower(FeatureFlag::valueToString($value)) + ); } if ($operator == "not_ends_with") { - return !str_ends_with(strtolower(FeatureFlag::valueToString($overrideValue)), strtolower(FeatureFlag::valueToString($value))); + return !str_ends_with( + strtolower(FeatureFlag::valueToString($overrideValue)), + strtolower(FeatureFlag::valueToString($value)) + ); } if (in_array($operator, ["regex", "not_regex"])) { @@ -771,7 +789,10 @@ private static function wildcardBounds($value): array $parts = explode(".", $text); // Remove trailing wildcard parts and empty parts - while (count($parts) > 0 && (end($parts) === "*" || end($parts) === "x" || end($parts) === "X" || end($parts) === "")) { + while ( + count($parts) > 0 + && (end($parts) === "*" || end($parts) === "x" || end($parts) === "X" || end($parts) === "") + ) { array_pop($parts); } @@ -1411,7 +1432,8 @@ private static function isConditionMatch( $propertyMatchingVersion, $identityAggregation ): ConditionMatch { - $rolloutPercentage = array_key_exists("rollout_percentage", $condition) ? $condition["rollout_percentage"] : null; + $rolloutPercentage = array_key_exists("rollout_percentage", $condition) + ? $condition["rollout_percentage"] : null; if (count($condition['properties'] ?? []) > 0) { // Filters in a condition have no defined order, so an inconclusive filter does not stop