I have two carbon objects
//$startDate
Carbon\Carbon @1667250000 {#3093 ?�?
date: 2022-11-01 00:00:00.0 Africa/Nairobi (+03:00)
}
and
//$endDate
Carbon\Carbon @1669842000 {#2920 ?�?
date: 2022-12-01 00:00:00.0 Africa/Nairobi (+03:00)
}
when I run dateDiffInMonths() on them I get 0 but I expect 1
$startDate->diffInMonths($endDate); //0
I have tried adding floorMonth() to objects but the answer is still the same.
When I get$startDate->diff($endDate)
I get the following with days difference as 30
DateInterval {#3102 ?�?
interval: + 30d
+"y": 0
+"m": 0
+"d": 30
+"h": 0
+"i": 0
+"s": 0
+"f": 0.0
}
What am I doing wrong?
There are always bugs with DateTime in various PHP versions. These also affect carbon. I've been using this algorithm for years (from https://github.com/jspit-de/dt/blob/master/Dt.php#L1284):
function diffInMonth(DateTime $start,DateTime $end): int {
$endDate = (clone $end)->setTimeZone($start->getTimeZone());
list($yearStart,$monthStart,$dayStart) = explode(" ",$start->format("Y m dHis"));
list($yearEnd,$monthEnd, $dayEnd) = explode(" ",$endDate->format("Y m dHis"));
$mothDiff = ($yearEnd - $yearStart) * 12 + $monthEnd - $monthStart;
if($dayStart > $dayEnd && $end >= $start) --$mothDiff;
elseif($dayStart < $dayEnd && $end < $start) ++$mothDiff;
return $mothDiff;
}
$startDate = new DateTime('2022-11-01 00:00:00.0', new DateTimeZone('Africa/Nairobi'));
$endDate = new DateTime('2022-12-01 00:00:00.0', new DateTimeZone('Africa/Nairobi'));
$month = diffInMonth($startDate, $endDate);
echo ' Positve: '.$month."\n"; //1
$month = diffInMonth($endDate,$startDate);
echo " Negative: ".$month; //-1
Demo + Test: https://3v4l.org/Tgqcf
This function works the same way with carbon objects.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.