Q: Check which datetime is older if they have different format PHP How to check which date is older if they have different format, for example :
$date1 = "2018-05-09 12:31:51"; // "Y-m-d H:i:s" format
$date2 = "06/09/2018"; // "d/m/Y" format


A: DateTime::createFromFormat is pretty handy here 
Try this:
$date1 = "2018-05-09 12:31:51"; // "Y-m-d H:i:s" format
$date2 = "06/09/2018"; // "d/m/Y" format

$objDateA = DateTime::createFromFormat('Y-m-d H:i:s', $date1);
$objDateB = DateTime::createFromFormat('d/m/Y', $date2);


if ($objDateA < $objDateB)
{
    ...
}


A: The easiest way is to use a library called Carbon. (https://carbon.nesbot.com/docs/)
You can do it like this:
use Carbon\Carbon;
$date1 = "2018-05-09 12:31:51";
$date1 = Carbon::createFromFormat('Y-d-m H:i:s', $date1);

$date2 = "06/09/2018"; // "d/m/Y" format
$date2 = Carbon::createFromFormat('d-m-Y', $date2);

if ($date1 < $date2) {
     echo "Date 1 is older than Date 2";
else {
     echo "Date 1 is newer than Date 2";
}


A: Try this.
$date1_dt = new DateTime($date1);
$date2_dt = new DateTime($date2);

if ($date1_dt > $date2_dt) 
 { /* Do something */ }

It might possible that the date string is not supported by DateTime parser so you can initialize your dates like this.
$date1_dt = DateTime::createFromFormat('Y-m-d H:i:s', $date1);
$date2_dt = DateTime::createFromFormat('d/m/Y', $date2);

 if ($date1_dt > $date2_dt) 
 { /* Do something */ }

