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
    • 299
    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

    $date1 = '2016-08-31';
    $date2 = '2016-04-04';
    
    //Calling function to get date difference in year, month and days format
    $dataDifference = getDateDifference($date1, $date2)
    
    echo "Year : ".$dataDifference['years'];
    echo "Month : ".$dataDifference['months'];
    echo "Days : ".$dataDifference['days'];
    
    
    /*
    * Get date difference
    * @Param 1 : from date
    * @Param 2 : to date
    * @Return  : array of date difference
    */
    function getDateDifference($date1, $date2){
        $data = array();
        $diff = abs(strtotime($date2) - strtotime($date1));
        $data['years'] = floor($diff / (365*60*60*24));
        $data['months'] = floor(($diff - $data['years'] * 365*60*60*24) / (30*60*60*24));
        $data['days'] = floor(($diff - $data['years'] * 365*60*60*24 - $data['months']*30*60*60*24)/ (60*60*24));
            return $data;
       }
    

     

 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: