From 1da9f08b98e08afed65174e57f89fc8674cfacb5 Mon Sep 17 00:00:00 2001 From: Drew Johnston Date: Tue, 7 May 2024 14:19:20 -0500 Subject: [PATCH] Fix for passing in a Carbon 3 object 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. --- src/Recurr/DateUtil.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Recurr/DateUtil.php b/src/Recurr/DateUtil.php index 48a4e87..73de1f8 100644 --- a/src/Recurr/DateUtil.php +++ b/src/Recurr/DateUtil.php @@ -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++) {