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
    • 87
    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.

    $date1 = "2013-03-20";
    $date2 = "2015-06-30";
    
    $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);
    

    Another way is to get difference between two dates.

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

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: