Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find difference between two dates in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 109
    Comment on it

    We can easily find date difference in php by using strtotime which convert two dates to unix time once convert we can manipulate easily by calculating the number of seconds between them.Its the easy way to calculate different time periods.

    1. $date1 = "2013-03-20";
    2. $date2 = "2015-06-30";
    3.  
    4. $diff = abs(strtotime($date2) - strtotime($date1));
    5.  
    6. $years = floor($diff / (365*60*60*24));
    7. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    8. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    9.  
    10. printf("%d years, %d months, %d days\n", $years, $months, $days);

    Another way is to get difference between two dates.

    1. $date1 = new DateTime($date_1);
    2. $date2 = new DateTime($date_2);
    3. $interval = $date1->diff($date2);
    4. echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
    5.  
    6. // shows the total amount of days
    7. echo "difference " . $interval->days . " days ";

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: