How to compare two dates from MYSQL in php?

In this tutorial, we will explain to you how you can compare two dates from MYSQL in PHP.

1. Code:

 <?php 
	function compareTwoMysqlDate($date1, $date2){
		$d1 = new DateTime($date1);
		$d2 = new DateTime($date2);
		if ($d1 == $d2) {
			return 0;
		}
		if ($d1 > $d2) {
			return 1;
		}
		return -1;
	}
	
	$str = compareTwoMysqlDate("2018-11-15 00:00:00", "2018-11-11 00:00:00");	
	echo($str);
    ?> 

2. Example:

Input:

"2018-11-15 00:00:00",
"2018-11-11 00:00:00"

Output:

1