Function to check if a string is a date Function to check if a string is a date php php

Function to check if a string is a date


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}


Here's a different approach without using a regex:

function check_your_datetime($x) {    return (date('Y-m-d H:i:s', strtotime($x)) == $x);}