-
Date TimeStrings
Code function howManyTimesBetween(string $sentence): string { if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) { return 'Your question is strange'; } $dates = array_map('date_create', $dates); if ($dates['date1'] == $dates['date2']) { return 'Dates are equals'; } $diff = $dates['date1']->diff($dates['date2']); $diffStrings = [$diff->format('%y year(s)'), $diff->format('%m month(s)'), $diff->format('%d day(s)')]; $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT); return 'There are '.implode(', ', $diffStrings).' between '.$sentence; }
Test Cases <?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')); } }
Output:
-
Code - function howManyTimesBetween(string $sentence): string
- {
- if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) {
- return 'Your question is strange';
- }
- $dates = array_map('date_create', $dates);
- if ($dates['date1'] == $dates['date2']) {
- return 'Dates are equals';
- }
- $diff = $dates['date1']->diff($dates['date2']);
$diffStrings[] = $diff->format('%y year(s)');$diffStrings[] = $diff->format('%m month(s)');$diffStrings[] = $diff->format('%d day(s)');- $diffStrings = [$diff->format('%y year(s)'), $diff->format('%m month(s)'), $diff->format('%d day(s)')];
- $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT);
- return 'There are '.implode(', ', $diffStrings).' between '.$sentence;
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}