You must display literally the difference between two dates textual
Exemple input howManyTimesBetween("1995-12-02 and 2010-01-05")
Will return "There are 14 year(s), 10 month(s), 8 day(s) between 1995-12-02 and 2010-01-05"
Constraints and permissions
If constraints are not respected, the function should return "Your question is strange"
- The two dates can be inverted (the oldest can be at start or end), and will give the same gap between the twice dates
- If there are no months/day/years gap. Don't display it in the sentence (exemple :
howManyTimesBetween("2010-01-05 and 2010-01-10")
will return"There are 5 day(s) between 1995-12-02 and 2010-01-05"
) - If the two date are the same, will return "Dates are equals"
- The date format is Y-m-d (conforming to https://www.php.net/manual/fr/datetime.format.php). There is not hours or others
- The sentence should always do "{date} and {date}"
- The end of the response sentence will is the same than the input whatever the order of dates
function howManyTimesBetween(string $sentence): string
{
$errorMessage = 'Your question is strange';
$datesString = explode(' and ', $sentence);
if (count($datesString) !== 2) {
return $errorMessage;
}
$date1 = DateTime::createFromFormat('Y-m-d', $datesString[0]);
$date2 = DateTime::createFromFormat('Y-m-d', $datesString[1]);
if ($date1 === false or $date2 === false) {
return $errorMessage;
}
$diffStrings = [];
if ($date1 == $date2) {
return 'Dates are equals';
}
$diff = $date1->diff($date2);
if ($diff->y) {
$diffStrings[] = $diff->y.' year(s)';
}
if ($diff->m) {
$diffStrings[] = $diff->m.' month(s)';
}
if ($diff->d) {
$diffStrings[] = $diff->d.' day(s)';
}
return 'There are '.implode(', ', $diffStrings).' between '.$sentence;
}
<?php
use PHPUnit\Framework\TestCase;
/**
* test function names should start with "test"
*/
class HowManyTimesTest extends TestCase
{
public function test_simple(): void
{
$this->assertEquals('There are 14 year(s), 10 month(s), 3 day(s) between 1995-12-02 and 2010-10-05',
howManyTimesBetween('1995-12-02 and 2010-10-05'));
$this->assertEquals('There are 14 year(s), 10 month(s), 3 day(s) between 2010-10-05 and 1995-12-02',
howManyTimesBetween('2010-10-05 and 1995-12-02'));
$this->assertEquals('There are 3 day(s) between 2010-01-05 and 2010-01-08',
howManyTimesBetween('2010-01-05 and 2010-01-08'));
$this->assertEquals('There are 1 month(s), 3 day(s) between 2010-01-05 and 2010-02-08',
howManyTimesBetween('2010-01-05 and 2010-02-08'));
}
public function test_equals(): void
{
$this->assertEquals('Dates are equals', howManyTimesBetween('1995-12-02 and 1995-12-02'));
}
public function test_errors(): void
{
$this->assertEquals('Your question is strange', howManyTimesBetween('1995-12-02 and 2010-01-02 and 2010-01-05'));
$this->assertEquals('Your question is strange', howManyTimesBetween('1995-12-02 or 1995-12-02'));
$this->assertEquals('Your question is strange', howManyTimesBetween('2010-01-05 or 2010-01-05'));
$this->assertEquals('Your question is strange', howManyTimesBetween('1995-12-02,2010-01-05'));
$this->assertEquals('Your question is strange', howManyTimesBetween('...'));
$this->assertEquals('Your question is strange', howManyTimesBetween('02/04/1995 and 03/04/1995'));
$this->assertEquals('Your question is strange', howManyTimesBetween('1995-12-02 10:10 and 2010-01-05 11:11'));
$this->assertEquals('Your question is strange', howManyTimesBetween('1995-12-02 10:10 and 1995-12-02 10:10'));
}
}