Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to count date difference in day, month and year format

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 344
    Comment on it

    Hello friends,

    I have two date 2016-04-04 and 2016-08-31 and now I want to display its difference as 4 months 24 days format. I have created a function to return date difference in year, months and days format

    1. $date1 = '2016-08-31';
    2. $date2 = '2016-04-04';
    3.  
    4. //Calling function to get date difference in year, month and days format
    5. $dataDifference = getDateDifference($date1, $date2)
    6.  
    7. echo "Year : ".$dataDifference['years'];
    8. echo "Month : ".$dataDifference['months'];
    9. echo "Days : ".$dataDifference['days'];
    10.  
    11.  
    12. /*
    13. * Get date difference
    14. * @Param 1 : from date
    15. * @Param 2 : to date
    16. * @Return : array of date difference
    17. */
    18. function getDateDifference($date1, $date2){
    19. $data = array();
    20. $diff = abs(strtotime($date2) - strtotime($date1));
    21. $data['years'] = floor($diff / (365*60*60*24));
    22. $data['months'] = floor(($diff - $data['years'] * 365*60*60*24) / (30*60*60*24));
    23. $data['days'] = floor(($diff - $data['years'] * 365*60*60*24 - $data['months']*30*60*60*24)/ (60*60*24));
    24. return $data;
    25. }
    26.  

     

 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: