Date values are required to be compared in PHP for different purposes, such as calculating the deadline of a task and scheduling an event. It is easier to compare two dates of a similar format. If it is required to compare the date of different formats, then the date values will be required to convert into the same format using other built-in PHP functions before comparison. Different ways of comparing date values in PHP have been shown in this tutorial.
Compare Dates of Similar Format
The date values are stored as strings in PHP in the “yyyy-mm-dd” format. If this date value is not assigned in this format, then the wrong output will be generated. The way of comparing two dates of a similar format has been shown in this tutorial.
Example 1: Compare Date Values by Using a Conditional Statement
Create a PHP file with the following script that will read two date values in the PHP format from the URL parameters. Then print the message after comparing the date values using a conditional statement and comparison operator.
//Check whether the date values are passed in the URL parameter or not
if(isset($_GET['D1']) and isset($_GET['D2']))
{
//Set the first date value as a string
$dateVal1 = $_GET['D1'];
//Set the second date value as a string
$dateVal2 = $_GET['D2'];
//Check if the values are non-empty or not
if ($dateVal1 != "" and $dateVal2 != "")
{
//Compare the dates using comparison operator
if ($dateVal1 < $dateVal2)
echo "$dateVal1 is less than $dateVal2.";
else
echo "$dateVal1 is greater than $dateVal2.";
}
else
//Print error message
echo "One or two date values are empty.";
}
else
//Print error message
echo "One or more date values are missing.";
?>
The following output will appear after executing the previous script if no date value is passed as a URL parameter:
After executing the previous script, the following output will appear if “2022-09-21 and 2022-01-22” are passed in the URL parameter:
After executing the previous script, the following output will appear if 2022-09-21 and 2022-11-22” are passed in the URL parameter.
Example 2: Compare Date Values by Using the Ternary Operator
Create a PHP file with the following script that will read two date values in the PHP format from the URL parameters. Then print the message after comparing the date values using the ternary operator.
//Check whether the date values are passed in the URL parameter or not
if(isset($_GET['D1']) and isset($_GET['D2']))
{
//Set the first date value as a string
$dateVal1 = $_GET['D1'];
//Set the second date value as a string
$dateVal2 = $_GET['D2'];
//Check the values are non empty or not
if ($dateVal1 != "" and $dateVal2 != "")
//Compare the date values
echo $dateVal1 < $dateVal2 ? "$dateVal1 is less than $dateVal2." : "$dateVal1 is greater than $dateVal2.";
else
//Print error message
echo "One or two date values are empty.";
}
else
//Print error message
echo "One or more date values are missing.";
?>
After executing the previous script, the following output will appear if “2022-08-25 and 2022-11-22” are passed in the URL parameter:
Compare Date Objects of Similar or Different Formats
The way of comparing the date objects of the same or different formats has been shown in this part of the tutorial.
Example 3: Compare Date Objects of Similar Format
Create a PHP file with the following script to create two date objects of similar formats using the DateTime class. Next, the conditional statement was used to compare the dates.
//Assign the first date object
$dateVal1 = new DateTime("21 May 2022");
//Assign the second date object
$dateVal2 = new DateTime("16 Dec 2022");
//Compare the date values by using the comparison operator
if($dateVal1 > $dateVal2)
echo date_format($dateVal1,"d-M-Y"), " is greater than ", date_format($dateVal2,"d-M-Y"),".";
else
echo date_format($dateVal1,"d-M-Y"), " is less than ", date_format($dateVal2,"d-M-Y"),".";
?>
The following output will appear after executing the previous script:
Example 4: Compare Date Objects of Different Formats
Create a PHP file with the following script to create two date objects of different formats using the DateTime class. Next, the conditional statement has been used to compare the dates.
//Check event date is set or not
if(isset($_GET['ed']))
{
if($_GET['ed'] != "" )
{
//Read the current date
$today = new DateTime();
//Read the event date from the URL parameter
$eventDate = new DateTime($_GET['ed']);
//Calculate the difference between the dates
$difference = $today->diff($eventDate);
/*
A positive number will be returned
if the event date is greater than the current date
*/
if ($difference->format("%r%a") > 0 )
echo "The event is coming within ", $difference->days, " days.";
else
echo "The event was organized before ", $difference->days, " days.";
}
else
//Print error message
echo "The event date is empty.";
}
else
//Print error message
echo "The event date is missing.";
?>
After executing the previous script, the following output will appear if no date value is passed as a URL parameter:
After executing the previous script, the following output will appear if “2022-10-15” is passed in the URL parameter:
After executing the previous script, the following output will appear if “2022-01-15” is passed in the URL parameter:
Example 5: Compare Date Objects of Different Formats by Using the date_create() Function
Create a PHP file with the following script to create two date objects of different formats using the date_create() function. Next, the conditional statement has been used to compare the dates.
//Assign the first date by using date_create() function
$dateVal1 = date_create("30-10-2022");
//Assign the second date by using date_create() function
$dateVal2 = date_create("16 Dec 2022");
//Compare the date values of different format
if($dateVal1 > $dateVal2)
echo $dateVal1->format("d M Y"), " is greater than ", $dateVal2->format("d M Y"), "." ;
else
echo $dateVal1->format("d M Y"), " is less than ", $dateVal2->format("d M Y"), "." ;
?>
The following output will appear after executing the previous script.
Compare Date Values Based on the Timestamp Values
The way of comparing date values based on timestamp values has been shown in the following example:
Example 6: Compare Dates by Using the strtotime() Function
Create a PHP file with the following script that will compare two date values by converting the dates into the timestamp values:
The following output will appear after executing the previous script:
Conclusion
Different ways of comparing two date values of similar or different formats have been shown in this tutorial. PHP users can follow any method to compare date values based on their requirements.