Home >>PHP Reference >How to Get Difference between two dates

How to Get Difference between two dates

Difference between two dates in PHP

PHP Script

<?php 
extract($_POST);
if(isset($findDiff))
{

$Datediff             = abs(strtotime($date1) - strtotime($date2));

$years                   =   floor($Datediff / (365*60*60*24));
$months               =   floor(($Datediff - $years * 365*60*60*24) / (30*60*60*24));
$days                    =   floor(($Datediff- $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

echo "<h1>Differece between dates : </h1>";
echo $years."Years"." ".$months."Months"." ".$days. "Days";
}
?>

HTML Form(Select your two Dates )

<!DOCTYPE html>
<html>
<head>
<title>How to Get Difference between two dates</title>

</head>

<body>
	<form method="post">
		<table width="400" border="2" cellspacing="5" cellpadding="5">
  <tr>
    <th>Select First Date</th>
    <td><input type="date" name="date1" placeholder="Select Date1"/></td>
  </tr>
 <tr>
    <th>Select Second Date</th>
    <td><input type="date" name="date2" placeholder="Select Date2"/></td>
 </tr>
  <tr>
    <th colspan="2" scope="row">
	<input type="submit" name="findDiff" value="Find Difference"/>
	</th>
    </tr>
</table>

	</form>
</body>
</html>

Popular Tutorials