开发者

Regular expression to validate date in php

开发者 https://www.devze.com 2023-03-19 17:10 出处:网络
Can anyone help me with the regular expression to find datime from string,format like September 29th, 2010, 0开发者_C百科5:18 UTC

Can anyone help me with the regular expression to find datime from string,format like

September 29th, 2010, 0开发者_C百科5:18 UTC


Depends on how strict you want to validate.

^\w+\s+\w+,\s+\d+,\s+\d+:\d+\s+[\w:+-]*$

would work but wouldn't check if the dates are actually halfway valid. Date validation is notoriously hairy in regex, so I'd advise against that. Better try and parse the date and see if that throws an exception.


There are about as many possible REGEXes as there are people writing them. This one should work, assuming the format is relatively strict.

^([\w]+) ([^,]+), ([^,]+), ([^ ]+) (.*)$

If you're using PHP though, I'd rather just use strtotime() and work from there, it's a lot safer to guarantee you'll get a useful answer out of it. Just remember to config your default timezone.

0

精彩评论

暂无评论...
验证码 换一张
取 消