How to check format of date in php?

In this tutorial, we are going to see how to check Date format in PHP.

1. Code:

<?php 	
	function isCheckDateWithFormat($date, $format = 'Y-m-d'){
		$d = DateTime::createFromFormat($format, $date);
		return $d && $d->format($format) === $date;
	}
	$date = "22-06-2022";
	var_dump(isCheckDateWithFormat($date));
    ?> 

2. Example:

Input:

"22-06-2022", 'Y-m-d'

Output:

bool(false)