What would be the best approach to finding a date in a freeform text? A post where a user may place a date in it in several different ways such as:
- July 14th & 15th
- 7/14 & 7/15
- 7-14 & 7-15
- Saturday 14th and Sunday 15th
- Saturday July 14th and 15th
and so on. Is regex my best choice for this type of thing with preg_match? I would also like to search if there are two dates, one for a start date and a second for an end date, but in the text I’m searching there may be one date or two.
This is my PHP code so far:
$dates1 = '01-01';
$dates2 = 'July 14th & 15th';
$dates3 = '7/14 & 7/15';
$dates4 = '7-14 & 7-15';
$dates5 = 'Saturday 14th and Sunday 15th';
$dates6 = 'Saturday July 14th and 15th';
$regexes = array(
'/\s(1|2|3|4|5|6|7|8|9|10|11|12)\/\d{1,2}/', //finds a date
'/\s(1|2|3|4|5|6|7|8|9|10|11|12)-\d{1,2}/', //finds another date
'%\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])\b%', //finds date format dd-mm or dd.mm
);
foreach($regexes as $regex){
preg_match($regex,$dates,$matches);
}
var_dump($matches);
PHP has a class called DateTime that manages timestamps. It lets you convert between strings and DateTime objects pretty easily… Providing that your strings use the formats PHP gives you.
For example,
That said, here’s what I’d do:
Create an array of acceptable formats, in order of priority:
Work with RegEx to massage your inputs so that they match your formats.
Try to construct a DateTime object: