简体   繁体   中英

php - How to check “March 21, 2015” string is a valid date?

I am having a variable which holds date as string like March 21, 2015,so I need to check whether it is a valid in php.I tries to check using

$date = 'July 20 2018'; echo date('d/m/Y', strtotime($date)) $date = 'July 20 2018'; echo date('d/m/Y', strtotime($date)) ;

But it showing error and also tried

if (DateTime::createFromFormat('Y-m-d G:i:s', $myString) !== FALSE) { //valid date }

Getting error 2nd parameter must be string. Please anybody can help me !

strtotime will parse the date string to a number: please refer to this.

so the returned value from strtotime cannot be passed to createfromFromat as the second parameter.

You can use the following code to verify the date

$date = DateTime::createFromFormat('July 25 2010', 'F j Y');
return $date && ($date->format('F j Y') === 'July 25 2010');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM