Date Time
Strings
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 (!($diff = $dates['date1']->diff($dates['date2']))->days) { return 'Dates are equal'; } $years = $diff->format('%y'); $months = $diff->format('%m'); $days = $diff->format('%d'); $hours = $diff->format('%h'); $minutes = $diff->format('%i'); $seconds = $diff->format('%s'); $timeDetails = []; if ($years) { $timeDetails[] = "$years year(s)"; } if ($months) { $timeDetails[] = "$months month(s)"; } if ($days) { $timeDetails[] = "$days day(s)"; } if ($hours) { $timeDetails[] = "$hours hour(s)"; } if ($minutes) { $timeDetails[] = "$minutes minute(s)"; } if ($seconds) { $timeDetails[] = "$seconds second(s)"; } $totalDays = ($diff->y * 365) + ($diff->m * 30) + $diff->d; $averagePerDay = ($totalDays > 0) ? round($totalDays / $diff->days, 2) : 0; $output = 'There are ' . implode(', ', $timeDetails) . " between $sentence."; $output .= " On average, that's approximately $averagePerDay time interval(s) per day."; return $output; }
- 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 (!($diff = $dates['date1']->diff($dates['date2']))->days) {
return 'Dates are equals';- return 'Dates are equal';
- }
- $years = $diff->format('%y');
- $months = $diff->format('%m');
- $days = $diff->format('%d');
- $hours = $diff->format('%h');
- $minutes = $diff->format('%i');
- $seconds = $diff->format('%s');
- $timeDetails = [];
- if ($years) {
- $timeDetails[] = "$years year(s)";
- }
- if ($months) {
- $timeDetails[] = "$months month(s)";
- }
- if ($days) {
- $timeDetails[] = "$days day(s)";
- }
- if ($hours) {
- $timeDetails[] = "$hours hour(s)";
- }
- if ($minutes) {
- $timeDetails[] = "$minutes minute(s)";
- }
- if ($seconds) {
- $timeDetails[] = "$seconds second(s)";
- }
- $totalDays = ($diff->y * 365) + ($diff->m * 30) + $diff->d;
- $averagePerDay = ($totalDays > 0) ? round($totalDays / $diff->days, 2) : 0;
- $output = 'There are ' . implode(', ', $timeDetails) . " between $sentence.";
- $output .= " On average, that's approximately $averagePerDay time interval(s) per day.";
$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";- return $output;
- }
<?php use PHPUnit\Framework\TestCase; 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. On average, that\'s approximately 1 time interval(s) per day.', 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. On average, that\'s approximately 1 time interval(s) per day.', howManyTimesBetween('2010-10-05 and 1995-12-02') ); $this->assertEquals( 'There are 3 day(s) between 2010-01-05 and 2010-01-08. On average, that\'s approximately 1 time interval(s) per day.', 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. On average, that\'s approximately 0.97 time interval(s) per day.', // Rounded value here howManyTimesBetween('2010-01-05 and 2010-02-08') ); } public function test_equals(): void { $this->assertEquals( 'Dates are equal', 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') ); // ... (add other error tests) } }
- <?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_simple(): void
- {
- $this->assertEquals(
- 'There are 14 year(s), 10 month(s), 3 day(s) between 1995-12-02 and 2010-10-05. On average, that\'s approximately 1 time interval(s) per day.',
- 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. On average, that\'s approximately 1 time interval(s) per day.',
- howManyTimesBetween('2010-10-05 and 1995-12-02')
- );
- $this->assertEquals(
- 'There are 3 day(s) between 2010-01-05 and 2010-01-08. On average, that\'s approximately 1 time interval(s) per day.',
- 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. On average, that\'s approximately 0.97 time interval(s) per day.', // Rounded value here
- 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'));- $this->assertEquals(
- 'Dates are equal',
- 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'));- $this->assertEquals(
- 'Your question is strange',
- howManyTimesBetween('1995-12-02 and 2010-01-02 and 2010-01-05')
- );
- // ... (add other error tests)
- }
- }