PHP Date compare the dates, PHP Compare dates example


 

PHP Date compare the dates, PHP Compare dates example

In this section we will show you how you can compare dates in your php program. We have compare the current dates with future and old dates.

In this section we will show you how you can compare dates in your php program. We have compare the current dates with future and old dates.

The PHP Date compare the dates example code

In this PHP Compare dates example, we are describing the code to compare the dates. In the php program we have created the variables for current date, future date and old date. Then, we have compared these dates according to current date with future date as well as current dates with the past dates.

Here the example code to compare the dates in PHP Program.

<?php

//This example code show you how to compare two difference dates in PHP

echo "<b>PHP Date compare example</b><br>";

$firstDate = date("Y-m-d");// First date which is current date

echo "First Date: ".$firstDate."<br>";

//We will be using strtotime function to convert date into time and the compare

$firstDateinTime=strtotime($firstDate);

//Create second date by adding 2 days

$secondDate = strtotime(date("Y-m-d", strtotime($firstDate)) . " +2 day");

echo "Second date: ".date('l dS \o\f F Y', $secondDate)."<br>";

//Create third date by substracting 2 days

$thirdDate = strtotime(date("Y-m-d", strtotime($firstDate)) . " -2 day");

echo "Third date: ".date('l dS \o\f F Y', $thirdDate)."<br>";

//Now compare these three days

if($firstDateinTime > $secondDate){

echo "<br>First Date($firstDateinTime) is greater then second date($secondDate).";

}else{

echo "<br>First Date($firstDateinTime) is smaller then second date($secondDate).";

}

if($firstDateinTime > $thirdDate){

echo "<br>First Date($firstDateinTime) is greater then third date($thirdDate).";

}else{

echo "<br>First Date($firstDateinTime) is smaller then third date($thirdDate).";

}

?>

If you the above program you will get the following output:

PHP Date compare example
First Date: 2009-09-01
Second date: Thursday 03rd of September 2009
Third date: Sunday 30th of August 2009

First Date(1251763200) is smaller then second date(1251936000).
First Date(1251763200) is greater then third date(1251590400).

Ads