How to get number of days between two dates in php?


Answers

  • April 21, 2020

    We can get the days between two dates by below code.

     

    <?php
    $today= date('d-m-Y'); // get today's date
    // here you can also change today's date according to your requirement
    
    $timestamp= date('Y-m-d',strtotime($fetch['timestamp']));
    // here timestamp value has been retrieved from database 
    
    $datetime1 = new DateTime("$timestamp");
    $datetime2 = new DateTime("$today");
    $difference = $datetime1->diff($datetime2); // compare both dates
    
    $days= 'Difference: '.$difference->days.' days';
    // get the days difference in a variable
    
    echo $days;  // Print the days where you want to show
    
    // You can also get the difference in year, months
    // to get the year difference, use below code
    
    $years= 'Difference: '.$difference->y.' years';
    
    echo $years;
    
    // to get the month difference, use below code
    
    $months= 'Difference: '.$difference->m.' months';
    
    echo $months;
    ?>

     

    Comment

Have More Question?

Subscribe For Daily Updates

Flutter Questions
Android Questions