IT & Programming

Get days between two dates with PHP

  • How to get number of days between two dates.
  • Calculate total days between two dates.

Function accepts two date parameters and returns the total number of dates between them.

Function

function count_days( $a, $b ) {

    $gd_a = getdate( $a );
    $gd_b = getdate( $b );
    $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] );
    $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
    return round( abs( $a_new - $b_new ) / 86400 );
}

Calling The Function

$date1 = strtotime('2010/04/07');
$date2 = strtotime('2010/04/17');
$total_days = count_days($date1,$date2);

Output

10 Days

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *