Finding the number of days between two dates

后端 未结 30 2512
心在旅途
心在旅途 2020-11-21 23:47

How to find number of days between two dates using PHP?

相关标签:
30条回答
  • 2020-11-22 00:23
    $early_start_date = date2sql($_POST['early_leave_date']);
    
    
    $date = new DateTime($early_start_date);
    $date->modify('+1 day');
    
    
    $date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
    $date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);
    
    $interval = date_diff($date_a, $date_b);
    
    
    $time = $interval->format('%h:%i');
    $parsed = date_parse($time);
    $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60;
    //        display_error($seconds);
    
    $second3 = $employee_information['shift'] * 60 * 60;
    
    if ($second3 < $seconds)
        display_error(_('Leave time can not be greater than shift time.Please try again........'));
        set_focus('start_hr');
        set_focus('end_hr');
        return FALSE;
    }
    
    0 讨论(0)
  • 2020-11-22 00:24

    If you have the times in seconds (I.E. unix time stamp) , then you can simply subtract the times and divide by 86400 (seconds per day)

    0 讨论(0)
  • 2020-11-22 00:24

    number of days between two dates in PHP

          function dateDiff($date1, $date2)  //days find function
            { 
                $diff = strtotime($date2) - strtotime($date1); 
                return abs(round($diff / 86400)); 
            } 
           //start day
           $date1 = "11-10-2018";        
           // end day
           $date2 = "31-10-2018";    
           // call the days find fun store to variable 
           $dateDiff = dateDiff($date1, $date2); 
    
           echo "Difference between two dates: ". $dateDiff . " Days "; 
    
    0 讨论(0)
  • 2020-11-22 00:25

    You can try the code below:

    $dt1 = strtotime("2019-12-12"); //Enter your first date
    $dt2 = strtotime("12-12-2020"); //Enter your second date
    echo abs(($dt1 - $dt2) / (60 * 60 * 24));
    
    0 讨论(0)
  • 2020-11-22 00:25
    <?php
    $date1=date_create("2013-03-15");
    $date2=date_create("2013-12-12");
    $diff=date_diff($date1,$date2);
    echo $diff->format("%R%a days");
    ?>
    

    used the above code very simple. Thanks.

    0 讨论(0)
  • 2020-11-22 00:27

    From PHP Version 5.3 and up, new date/time functions have been added to get difference:

    $datetime1 = new DateTime("2010-06-20");
    
    $datetime2 = new DateTime("2011-06-22");
    
    $difference = $datetime1->diff($datetime2);
    
    echo 'Difference: '.$difference->y.' years, ' 
                       .$difference->m.' months, ' 
                       .$difference->d.' days';
    
    print_r($difference);
    

    Result as below:

    Difference: 1 years, 0 months, 2 days
    
    DateInterval Object
    (
        [y] => 1
        [m] => 0
        [d] => 2
        [h] => 0
        [i] => 0
        [s] => 0
        [invert] => 0
        [days] => 367
    )
    

    Hope it helps !

    0 讨论(0)
提交回复
热议问题