0

I'm trying to get days count between two dates from database. Date_from and Date_to. And I'm getting an error. Any help would be appreciated. The code:

<?php
    require('config/conn.php');

    // Select all from table 'reguests'
    $query = 'SELECT * FROM requests';

    //Results from table
    $result = mysqli_query($conn, $query);

    //Fetch data
    $requests = mysqli_fetch_all($result, MYSQLI_ASSOC);

    //Free result from fetch
    mysqli_free_result($result);

    //Get Days Count Between Dates From And To
    $dateFrom = $request['date_from'];
    $dateTo = $requests['date_to'];

    $daysDiff = floor(abs(strtotime($dateTo) - strtotime($dateFrom)) / (60*60*24));


    //Close conn
    mysqli_close($conn);
    ?>

Output:

<td><?php echo $daysDiff ?>

2 Answers2

1

Your question title says to exclude weekends, but your code doesn't seem to try to?

To account for more complicated logic like that it's probably prudent to calculate a period and use it to iterate over days.

Something like this:

$dateFrom = new DateTime();
$dateTo   = new DateTime( '+1 month +1 second' ); // Add 1s so period includes last day.

$period   = new DatePeriod( $dateFrom, new DateInterval( 'P1D' ), $dateTo );
$days     = 0;

foreach ( $period as $date ) {

    $day = $date->format( 'l' );

    if ( 'Saturday' !== $day && 'Sunday' !== $day ) {
        $days ++;
    }
}

echo $days; // 23
Rarst
  • 2,335
  • 16
  • 26
0

Please review your title, it's ambiguous with your problem. But to check difference between dates, please check the Php manual on the link: http://php.net/manual/en/datetime.diff.php

If you really wanna check if a day is weekend, please check: Checking if date is weekend PHP

Pablo Castro
  • 134
  • 6