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
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 H:i:s', $myString) !== false) { // it's a date }
Easiest way to check if a string is a date:
if(strtotime($date_string)){ // it's in date format }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With