PHP: Date Difference for PHP 5.2

function days_diff($d1, $d2) {

    $x1 = days($d1);
    $x2 = days($d2);  

    if ($x1 && $x2) {
        return abs($x1 - $x2);
    }

}

function days($x) {

    if (get_class($x) != 'DateTime') {

        return false;

    }  

    $y = $x->format('Y') - 1;
    $days = $y * 365;
    $z = (int)($y / 4);
    $days += $z;
    $z = (int)($y / 100);
    $days -= $z;
    $z = (int)($y / 400);
    $days += $z;
    $days += $x->format('z');

    return $days;

}

// Call function
$start_date = '2015-08-19 15:18:19';
$end_date = '2015-08-22 15:18:19';

$date1=date_create(date('Y-m-j',strtotime($start_date)));
$date2=date_create(date('Y-m-j',strtotime($end_date)));
$diff=date_diff($date1,$date2);

echo $diff;

Comments

Popular posts from this blog

HTML5 video loop src change on end play function

Custom OpenCart 2.3.0.2 theme