Calculate the difference between two dates using PHP

The simplest way to calculate the difference between two dates is…

Bazi-Date-Selection11

Function :

<?php
function date_difference($start_date, $end_date) {    
    $date1 = new DateTime($start_date);
    $date2 = new DateTime($end_date);
    $interval = $date1->diff($date2);
    echo "difference " . $interval->y . " year(s), " . $interval->m." month(s), ".$interval->d." day(s) ";
}
?>

usage :

<?php date_difference("2012-07-12","2012-07-13"); ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s