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
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
Edited by Barand
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.