Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract date from string in format "DD MMM YYYY"

I am trying to extract a date from a string variable and was hoping to get some help.

$editdate = "Content last modified on 17 May 2011 at 23:13";

from this string, I am trying to extract 17 May 2011, please keep in mind that the date will vary and the code needs to be able to extract any date in this format, DD MMM YYYY.

I thought of using preg_match to do this but I couldn't come up with a proper regex pattern that would extract the date properly.

Is this possible to do with regex or should I use a different function?

Thanks for the help !

like image 952
Kevin Jung Avatar asked Dec 05 '22 22:12

Kevin Jung


1 Answers

Try:

$timestamp = strtotime( str_replace( array("Content last modified ", "at"), "", $editdate ) );

Which will leave you with an epoch time stamp that you can then output however you like using date()

like image 172
stevecomrie Avatar answered Dec 10 '22 10:12

stevecomrie