PHP - Count number of days between 2 dates

后端 未结 3 700
南笙
南笙 2021-01-25 16:49

I am trying to write a function that can count the number of days between 2 dates. I currently have the below but it is giving me some unexpected results:

functi         


        
相关标签:
3条回答
  • 2021-01-25 17:12

    please use date_diff function like this

    <?php
    $date1=date_create("2013-03-15");
    $date2=date_create("2013-12-12");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    ?>
    

    hope this helps you thanx.

    0 讨论(0)
  • 2021-01-25 17:27

    You can also use this (for versions PHP 5.1.5 and above without using date_diff()):

    function dayCount($from, $to) {
        $first_date = strtotime($from);
        $second_date = strtotime($to);
        $days_diff = $second_date - $first_date;
        return date('d',$days_diff);
    }
    
    print dayCount($s, $e).' Days';
    
    0 讨论(0)
  • 2021-01-25 17:35

    Please use diff function for get days between two date.

    $date1 = new DateTime("25-03-2016");
    $date2 = new DateTime("01-04-2016");
    
    echo $diff = $date2->diff($date1)->format("%a");
    
    0 讨论(0)
提交回复
热议问题