Dates/Time
Data Types
Strings
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)'; } if( ($diff->y && $diff->m) || $diff->m) return 'There are '.implode(', ', $diffStrings).' between '.$sentence; elseif($diff->d) return "There are ".$diff->d." day(s) between ".$sentence; }
- 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;- if( ($diff->y && $diff->m) || $diff->m) return 'There are '.implode(', ', $diffStrings).' between '.$sentence;
- elseif($diff->d) return "There are ".$diff->d." day(s) between ".$sentence;
- }