Move History

Fork Selected
  • Dates/Time
    Data Types
    Strings
    Code
    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;
        }
        
        $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;
    }
    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'));
        }
    }
    
  • Code
    • function howManyTimesBetween(string $sentence): string
    • {
    • $errorMessage = 'Your question is strange';
    • $dates = array_map(
    • fn($str) => DateTime::createFromFormat('Y-m-d', $str),
    • explode(' and ', $sentence)
    • );
    • $dates = array_filter($dates);
    • if (count($dates) !== 2) {
    • return $errorMessage;
    • if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) {
    • return $errorMessage;
    • }
    • $diffStrings = [];
    • if ($dates[0] == $dates[1]) {
    • $dates = array_map('date_create', $dates);
    • if ($dates['date1'] == $dates['date2']) {
    • return 'Dates are equals';
    • }
    • $diff = $dates[0]->diff($dates[1]);
    • $mapping = [
    • 'y' => 'year(s)',
    • 'm' => 'month(s)',
    • 'd' => 'day(s)',
    • ];
    • foreach ($mapping as $p => $s) {
    • if ($diff->$p) {
    • $diffStrings[] = $diff->$p.' '.$s;
    • }
    • }
    • $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;
    • }