Last active
September 19, 2018 13:02
-
-
Save adlerdias/3a0bbb29266991080ac360f1f019336e to your computer and use it in GitHub Desktop.
Check if a string is a valid date(time)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .... | |
// | |
// Usage Example: | |
// | |
// var_dump($this->isValidDateTimeString('2018-13-10', 'Y-m-d')); # false | |
// var_dump($this->isValidDateTimeString('2018-12-10', 'Y-m-d')); # true | |
// var_dump($this->isValidDateTimeString('10/13/2018')); # false | |
// var_dump($this->isValidDateTimeString('10/12/2018')); # true | |
/** | |
* Check if a string is a valid date(time) | |
* | |
* DateTime::createFromFormat requires PHP >= 5.3 | |
* | |
* @param string $date | |
* @param string $format | |
* @return bool | |
*/ | |
public function isValidDateTimeString(string $date, string $format = 'd/m/Y') : bool | |
{ | |
return (\DateTime::createFromFormat($format, $date) !== false) | |
&& (\DateTime::getLastErrors()["warning_count"] == 0 && \DateTime::getLastErrors()["error_count"] == 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment