Calculate experience in years, months and days from two given dates in PHP



In this tutorial, I will show you how to calculate total experience of your job in years, months and days from two given dates using PHP script.

<?php
    if(isset($_POST['submit'])){
        $datetime1 = new DateTime($_POST['date1']);
        $datetime2 = new DateTime($_POST['date2']);
        $interval = $datetime1->diff($datetime2);
        echo $interval->format('%y years %m months and %d days');
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Calculate experience in years, months and days from two given dates</title>
    </head>
    <body>
		<form method="post">
			<table width="80%">
				
				<tr>
					<td width="30%">Enter First Date : </td>
					<td><input id="date1" required type="text" name="date1" value="" readonly /></td>
				</tr>
				
				<tr>
					<td>Enter Second Date : </td>
					<td><input id="date2" required type="text" name="date2" value="" readonly /></td>
				</tr>
				
				<tr>
					<td>&nbsp;</td>
					<td><input name="submit" type="submit" value="Submit" /></td>
				</tr>
				
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				
			</table>
		</form>
		
		<script>
			$( "#date1" ).datepicker({
				dateFormat: "yy-mm-dd",
				changeMonth: true
			});

			$( "#date2" ).datepicker({
				dateFormat: "yy-mm-dd",
				changeMonth: true
			});
		</script>
	
    </body>
</html>

You can calculate the difference between two dates using above PHP script.

Live Demo