Sunday, 21 June 2015

how to get difference between two dates in php

<?php 
$date1="2011-01-02";
$date2=date("Y-m-d");

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

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

printf("%d years, %d months, %d days\n", $years, $months, $days);

?>


O/P -  4 years, 5 months, 20 days


No comments:

Post a Comment