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 ($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 = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT); return 'There are '.implode(', ', $diffStrings).' between '.$sentence; }
- function howManyTimesBetween(string $sentence): string
- {
$errorMessage = 'Your question is strange';- if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) {
return $errorMessage;- 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 = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT);
- return 'There are '.implode(', ', $diffStrings).' between '.$sentence;
- }