Skip to content

EspoCRM – Open Source CRM Application

Fix datetime\diff returning float for days, hours and minutes (#3799)

Leo Camus98551cda5178
diff --git a/application/Espo/Core/Formula/Functions/DatetimeGroup/DiffType.php b/application/Espo/Core/Formula/Functions/DatetimeGroup/DiffType.php
index 30e962878e..005ad2cbe0 100644
--- a/application/Espo/Core/Formula/Functions/DatetimeGroup/DiffType.php
+++ b/application/Espo/Core/Formula/Functions/DatetimeGroup/DiffType.php
@@ -119,11 +119,11 @@ class DiffType extends BaseFunction
if ($intervalType === 'seconds') {
$number = $secondsDiff;
} else if ($intervalType === 'minutes') {
- $number = floor($secondsDiff / 60);
+ $number = (int) floor($secondsDiff / 60);
} else if ($intervalType === 'hours') {
- $number = floor($secondsDiff / (60 * 60));
+ $number = (int) floor($secondsDiff / (60 * 60));
} else if ($intervalType === 'days') {
- $number = floor($secondsDiff / (60 * 60 * 24));
+ $number = (int) floor($secondsDiff / (60 * 60 * 24));
} else {
$property = $this->intervalTypePropertyMap[$intervalType];
$interval = $dateTime2->diff($dateTime1);
diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
index 17930952df..9da492ad6c 100644
--- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php
+++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
@@ -334,6 +334,21 @@ class EvaluatorTest extends TestCase
$this->assertEquals(-12, $this->evaluator->process($expression));
}
+ public function testDatetimeDiffReturnsInt(): void
+ {
+ $expression = "datetime\\diff('2022-01-04', '2022-01-01', 'days')";
+ $this->assertSame(3, $this->evaluator->process($expression));
+
+ $expression = "datetime\\diff('2022-01-01 10:30:00', '2022-01-01 08:00:00', 'hours')";
+ $this->assertSame(2, $this->evaluator->process($expression));
+
+ $expression = "datetime\\diff('2022-01-01 10:30:00', '2022-01-01 10:00:00', 'minutes')";
+ $this->assertSame(30, $this->evaluator->process($expression));
+
+ $expression = "datetime\\diff('2022-01-04', '2022-01-01', 'days') == 3";
+ $this->assertTrue($this->evaluator->process($expression));
+ }
+
public function testStringReplace()
{
$expression = "string\\replace('hello {test} hello', '{test}', 'hello')";