this my situation at the moment:
I save a dynamic string into a variable $content.
With this code I get all values of my $content which has the number format x.xx
preg_match_all('/\d+\,\d+/m', $content, $matches);
Now I would like to get additionally all values which are dates.
For example:
14.10.2021
10/14/2021
10. October 2021
The year could be also like 21 instead of 2021.
Here are many version possible.
Is there also a preg_match_all solution for that?
Or is there an way to say:
Give me all values of the string, where php found a valid date?
Thank you a lot !!
My Answer:
You can use the following regular expression to match date values in different formats:
php
preg_match_all('/\b(?:\d{1,2}[\/.]\d{1,2}[\/.]\d{2,4}|\d{1,2}\s(?:January|February|March|April|May|June|July|August|September|October|November|December)\s\d{2,4})\b/', $content, $matches);
This regular expression will match dates in the formats:
- dd/mm/yyyy or dd.mm.yyyy
- mm/dd/yyyy or mm.dd.yyyy
- dd Month yyyy (e.g. 10 October 2021)
You can adjust the regular expression as needed to match other date formats.
Rate this post
3 of 5 based on 9169 votesComments