How can i find date inside a string in PHP and extract it?’
example: I have a date in string
$status = "OK, 26.10.2022 13:24:00, Delivered Zak";
but also next time i can only get string which looks like this:
$status = "28.10.2022 11:20:00, Delivered";
So explode it to the pieces by "," is not a option here, because everytime is in different position. How can i get this date everytime?
Thanks for any suggestion.
2
Answers
You can use a regular expression to get the date in the string :
Output:
This solution uses trim to remove spurious characters before and after the date expression. A..Z removes all uppercase letters. Simply using date_create will also recognize some other date formats.
Output:
Demo: https://3v4l.org/uqAmm
If trim is not sufficient for special cases or if only certain date formats are to be accepted, a preg_replace can be used instead of trim.