Search code examples
phpdate-parsing

Find first date in string


I have a pdf file which i parsed using "pdfparser" plugin. From the text of the pages i need to find first date after a specific string (search string). I can find the search string and for date extraction i used

date_parse($string)

it extracts the date, month fine but i think due to the large string (with some more dates, numbers) it didn't populate the correct year, it gives a random number which is not even present in doc.

Is there any other way to get that date . Following is the example string.(it has more date after the date)

Satisfaction of the mortgage from Karen Ann Lewis,a single woman to Bank of America, N.A. recorded March 4, 2004

Solution

  • You could try the following RegEx (to extract the first date in the format you've provided), and then use parse_date():

    $str = 'Satisfaction of the mortgage from Karen Ann Lewis,a single woman to Bank of America, N.A. recorded March 4, 2004';
    
    preg_match("/(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}/", $str, $matches);
    
    var_dump( date_parse($matches[0]) );
    

    The above outputs:

    array(12) {
      ["year"]     => int(2004)
      ["month"]    => int(3)
      ["day"]      => int(4)
      ["hour"]     => bool(false)
      ["minute"]   => bool(false)
      ["second"]   => bool(false)
      ["fraction"] => bool(false)
      ...
    }
    

    Demo here