Extracting a date from a string with PHP

Hopefully this will be the first of many posts in this category. I was answering a question in Stackoverflow today about extracting dates from twitted messages and after all the debate that went on about what the best procedure was I decided to write my own function that will do exactly that, extract the most complex date from a string. By most complex I mean that it will prioritize something like “May 2nd 2008” over “May 2nd“. The function, named extractDateFromString (why make it less intuitive) returns an array containing the part of the string it decided was the best date, a UNIX datestamp of that date and a formatted date using either a user provided format or the predefined one.

It has some flaws, like the way I say that if the string length of the string that returns a valid datestamp is longer than a previously matching string, it should replace the old one. This could lead to saturday replacing sunday for example, and none of those is more valid that the other. This problem could be solver by storing all of those in an array and showing them in order. But it’s out of context as far as I’m concerned, it’s entirely up to you to do it.

Keep in mind that since strtotime only parses English strings, we can’t take advantage of it for other languages. This means, that you will only be able to use this to parse English messages in search of valid dates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
function extractDateFromString($datestring,$format = "F j, Y"){
    $datestring     = strtolower($datestring);
    $arr_datestring = explode(" ",$datestring);

    for($b=0;$b<count($arr_datestring);$b++){
        for($i=0;$i<count($arr_datestring);$i++){
            $tmp = $arr_datestring;
            if($b==0)$tmp = implode(array_splice($tmp, $i), " ");
            if($b>0) $tmp = implode(array_splice($tmp, $i,-$b), " ");

            if(strtotime($tmp) !== false){
                if(empty($result) || strlen($tmp) > strlen($result)) $result = $tmp;
            }
        }
    }
    if(empty($result)) return 'No dates found';
   
    $result = array('string' => $result,
                    'unix'   => strtotime($result),
                   'date'    => date($format,strtotime($result))
            );
    return $result;
}
?>

Examples of the output given by the function to some strings follow:
Input: “I’m going to play croquet next Friday”
Output: Array ( [string] => "next friday" [unix] => 1276844400 [date] => "June 18, 2010" )

Input: “Gadzooks, is it 17th June already?”
Output: Array ( [string] => "17th june" [unix] => 1276758000 [date] => "June 17, 2010" )

No posts at the moment. Check back again later!

About Juan

Hola! Me llamo Juan y soy un programador de 23 años de Madrid. Este es mi blog personal dedicado a la programación en general aunque con cierta predilección hacia PHP y Java.