diff --git a/CHANGELOG.md b/CHANGELOG.md index 73cd9e9..34230f7 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## [Unreleased] +## [2.2.1] - 2020-12-09 + +### Fixed + +- Fix support for `DateTimeImmutable` [#90](https://github.com/rlanvin/php-rrule/issues/90) + ## [2.2.0] - 2019-11-01 ### Added @@ -190,7 +196,8 @@ - First release, everything before that was unversioned (`dev-master` was used). -[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.2.0...HEAD +[Unreleased]: https://github.com/rlanvin/php-rrule/compare/v2.2.1...HEAD +[2.2.1]: https://github.com/rlanvin/php-rrule/compare/v2.2.0...v2.2.1 [2.2.0]: https://github.com/rlanvin/php-rrule/compare/v2.1.0...v2.2.0 [2.1.0]: https://github.com/rlanvin/php-rrule/compare/v2.0.0...v2.1.0 [2.0.0]: https://github.com/rlanvin/php-rrule/compare/v2.0.0-rc1...v2.0.0 diff --git a/src/RRule.php b/src/RRule.php index 5f867b7..3edda37 100755 --- a/src/RRule.php +++ b/src/RRule.php @@ -1350,10 +1350,10 @@ class RRule implements RRuleInterface $dtstart = clone $occurrence; // since DateTime is not immutable, clone to avoid any problem // so we skip the last occurrence of the cache if ($this->freq === self::SECONDLY) { - $dtstart->modify('+'.$this->interval.'second'); + $dtstart = $dtstart->modify('+'.$this->interval.'second'); } else { - $dtstart->modify('+1second'); + $dtstart = $dtstart->modify('+1second'); } } @@ -1367,7 +1367,7 @@ class RRule implements RRuleInterface // calculation magic at the end of the loop (when incrementing) // to realign on first pass. $tmp = clone $dtstart; - $tmp->modify('-'.pymod($dtstart->format('N') - $this->wkst,7).'days'); + $tmp = $tmp->modify('-'.pymod($dtstart->format('N') - $this->wkst,7).'days'); list($year,$month,$day,$hour,$minute,$second) = explode(' ',$tmp->format('Y n j G i s')); unset($tmp); } diff --git a/src/RRuleTrait.php b/src/RRuleTrait.php index a68c6bc..769a8a8 100755 --- a/src/RRuleTrait.php +++ b/src/RRuleTrait.php @@ -173,7 +173,7 @@ trait RRuleTrait * Convert any date into a DateTime object. * * @param mixed $date - * @return \DateTime + * @return \DateTimeInterface Returns a DateTimeImmutable if a DateTimeImmutable is passed, or DateTime otherwise * * @throws \InvalidArgumentException on error */ diff --git a/tests/RRuleTest.php b/tests/RRuleTest.php index 2c28583..f3cfe15 100755 --- a/tests/RRuleTest.php +++ b/tests/RRuleTest.php @@ -2779,6 +2779,41 @@ class RRuleTest extends TestCase $this->assertTrue($rule['DTSTART'] instanceof \DateTime); } + /** + * Test Bug #90 + * @see https://github.com/rlanvin/php-rrule/issues/90 + */ + public function testDateImmutable() + { + $dtstart_immutable = \DateTimeImmutable::createFromFormat('Y-m-d H:i', '2021-01-08 08:00'); + //$dtstart_mutable = \DateTime::createFromFormat('Y-m-d H:i', '2021-01-08 08:00'); + + $rrule = new RRule([ + 'BYDAY' => ['MO', 'WE', 'FR'], + 'FREQ' => 'WEEKLY', + 'WKST' => 'SU', + 'DTSTART' => $dtstart_immutable, + ]); + + $start = \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'); + $end = \DateTimeImmutable::createFromFormat('Y-m-d', '2021-12-31'); + + $occurrences = $rrule->getOccurrencesBetween($start, $end, 10); + + $this->assertEquals([ + new DateTime('Friday, January 8, 2021 08:00'), + new DateTime('Monday, January 11, 2021 08:00'), + new DateTime('Wednesday, January 13, 2021 08:00'), + new DateTime('Friday, January 15, 2021 08:00'), + new DateTime('Monday, January 18, 2021 08:00'), + new DateTime('Wednesday, January 20, 2021 08:00'), + new DateTime('Friday, January 22, 2021 08:00'), + new DateTime('Monday, January 25, 2021 08:00'), + new DateTime('Wednesday, January 27, 2021 08:00'), + new DateTime('Friday, January 29, 2021 08:00') + ], $occurrences, 'DateTimeImmutable produces valid results'); + } + /////////////////////////////////////////////////////////////////////////////// // Array access and countable interfaces