Skip to content

Commit

Permalink
Fix for passing in a Carbon 3 object
Browse files Browse the repository at this point in the history
Carbon 3 made some breaking changes here. (Specifically from the Laravel 11 upgrade)

If we're using Carbon, let's use the diffInDays method to get the difference in days.

Otherwise, we're probably using DateTime, so we'll use the diff method.

See: 
https://carbon.nesbot.com/docs/#api-carbon-3
https://www.php.net/manual/en/class.dateinterval.php

This definitely isn't the ultimate answer here - but it fixes the breaking change for my SaaS.
  • Loading branch information
drewjoh authored May 7, 2024
1 parent 1aff62e commit 1da9f08
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Recurr/DateUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ public static function getDaySetOfWeek(
$start = clone $dt;
$start = $start->setDate($start->format('Y'), 1, 1);

$diff = $dt->diff($start);
$start = $diff->days;
// Carbon 3 made some breaking changes here. (Specifically from the Laravel 11 upgrade)
// If we're using Carbon, let's use the diffInDays method to get the difference in days.
// Otherwise, we're probably using DateTime, so we'll use the diff method.
if(class_basename($start) == 'Carbon') {
// Carbon 3 also returns a float here now, so we cast it to an int to be more what we expect.
$diff_in_days = (int) $start->diffInDays($dt);
$start = $diff_in_days;
} else {
$diff = $dt->diff($start);
$start = $diff->days;
}

$set = array();
for ($i = $start, $k = 0; $k < 7; $k++) {
Expand Down

0 comments on commit 1da9f08

Please sign in to comment.