Michael_Dray Posted October 5, 2014 Share Posted October 5, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291457-extract-date-from-sentence/ Share on other sites More sharing options...
Barand Posted October 5, 2014 Share Posted October 5, 2014 (edited) 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 October 5, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/291457-extract-date-from-sentence/#findComment-1492819 Share on other sites More sharing options...
Michael_Dray Posted October 5, 2014 Author Share Posted October 5, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291457-extract-date-from-sentence/#findComment-1492821 Share on other sites More sharing options...
Barand Posted October 5, 2014 Share Posted October 5, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291457-extract-date-from-sentence/#findComment-1492825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.