This article demonstrates how to check if a given string is a valid date in PHP.

1. Using strtotime() function

You can determine if a string is a valid date using the strtotime() function. It returns the timestamp on success and false otherwise. It should be noted that the function doesn’t accept any custom date format but instead expects a string containing an English date format.

Download  Run Code

2. Using DateTime::format() function

If your date string contains a non-standard format, you can use the createFromFormat() function from the DateTime class to parse the strings. Then invoke the DateTime::format() function to determine if the date is formatted according to the given format. The following code example shows invocation of this function:

Download  Run Code

 
If you prefer the procedural style over the object-oriented style, you can use the date_create_from_format() and date_format() functions. These functions are the aliases of the DateTime::createFromFormat() and DateTime::format() functions, respectively.

Download  Run Code

That’s all there is to checking if a given string is a valid date in PHP.