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


$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>
<title>How to Get Difference between two dates</title>


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


Popular Tutorials