Jump to content

Extract Date From Sentence


Michael_Dray

Recommended Posts

Hi guys,

 

I have a selection on sentences that I have stored as strings.

 

They all contain dates in different formats. e.g.

 

 

"By Himanshu Singh on 30 July 2014"

"By Marc Shoffman - November 26th 2013, 3:47:09 pm"

"Tue Sep 23, 2014 6:33pm BST"

 

Is there a way to extract just the date from this and turn it into a standard format. For example dd/mm/yyyy

 

Many Thanks for any help

 

Mike

 

Link to comment
https://forums.phpfreaks.com/topic/291457-extract-date-from-sentence/
Share on other sites

I would recommend that, when you do convert to a standard format, the format should be yyyy-mm-dd.

 

Also, all those formats you gave will convert

<?php
$d1 = new DateTime("30 July 2014");
$d2 = new DateTime("November 26th 2013, 3:47:09 pm");
$d3 = new DateTime("Tue Sep 23, 2014 6:33pm BST");

echo $d1->format('Y-m-d');    //--> 2014-07-30
echo $d2->format('Y-m-d');    //--> 2013-11-26 
echo $d3->format('Y-m-d');    //--> 2014-09-23
?>

That just leaves the problem of finding the dates in the sentences. That bit will probably have to be done manually.

 

Perhaps a form with several pairs of fields.

  • Put the sentence in the first field of each pair
  • Copy/paste the date from the sentence to the second field of each pair.
  • Submit the form

 

To process,

  • convert the date as above
  • replace the date in the sentence with the converted date
  • output the new sentences

Many thanks for the quick reply. The dates will always be in the same format so for example the first results will always be:

By 'persons name' on 'date part'

 

The next

 

By 'persons name' - 'date part'

 

If the formats are the same is there a way I could use that to strip the Unrequired parts out before using your previously stated method to convert the date?

 

Many thanks again

Perhaps something like this

<?php
$sentences = array (
    "By William Shakespeare on 30 July 2014",
    "By John Doe Tue Sep 23, 2014 6:33pm BST",
    "By Isaac Asimov on November 26th 2013, 3:47:09 pm",
    "By Robert Ludlum - Tue Sep 23, 2014 6:33pm BST",
);

foreach ($sentences as $s) {
    if (strpos($s, ' on ', 0) !== false) {
        $split = ' on ';
    }
    elseif (strpos($s, ' - ', 0) !== false) {
        $split = ' - ';
    }
    else {
        echo $s . " (unchanged)<br>";
        continue;  // ignore and move on
    }
    
    list ($text, $date) = explode($split, $s);
    $d = new DateTime($date);
    $d2 = $d->format("Y-m-d");
    
    echo "$text$split$d2<br>";
}
?>

Which gives

By William Shakespeare on 2014-07-30
By John Doe Tue Sep 23, 2014 6:33pm BST (unchanged)
By Isaac Asimov on 2013-11-26
By Robert Ludlum - 2014-09-23

You should consider storing the the author and dates in separate fields.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.