检查字符串是否为日期的功能 [英] Function to check if a string is a date
本文介绍了检查字符串是否为日期的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个函数来确定字符串是否是使用PHP的日期/时间。基本上有效的日期/时间将如下所示:
I am trying to write a function to determine if a string is a date/time using PHP. Basically a valid date/time would look like:
2012-06-14 01:46:28
显然,尽管其完全动态的任何值都可以改变,但它应该总是以 XXXX的形式-XX-XX XX:XX:XX
,如何编写正则表达式来检查此模式,如果匹配则返回true。
Obviously though its completely dynamic any of the values can change, but it should always be in form of XXXX-XX-XX XX:XX:XX
, how can I write a regular expression to check for this pattern and return true if matched.
推荐答案
如果这是你的整个字符串,那么只需尝试解析它:
If that's your whole string, then just try parsing it:
if (DateTime::createFromFormat('Y-m-d G:i:s', $myString) !== FALSE) {
// it's a date
}
这篇关于检查字符串是否为日期的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文