Ad
Date Time
Strings
Code
Diff
  • 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;
    • }