Saturday, June 14, 2008

Get the date difference in the php (date_diff)

Get the difference between two dates in the php.
A custom function to get the date difference.

function get_datediff($dated_elimiter, $date_end, $date_start)
{
$arr_date_start=explode($dateformat, $date_start); //explode the start date by the delimiter
$arr_date_end=explode($dateformat, $date_end); //explode the start date by the delimiter
$startdate_greg=gregoriantojd($arr_date_start[1], $arr_date_start[2], $arr_date_start[0]); //use php function to converts a Gregorian date to Julian day count
$enddate_greg=gregoriantojd($arr_date_end[1],$arr_date_end[2],$arr_date_end[0]);
return $enddate_greg - $startdate_greg; // get the difference between the date
}

Now, let's get this code in action -

$date_end ="1986-10-06"; //value of the start date from where you want to start
$date_start= "1985-10-06"; //value of the start date up to where you want to count
$date_delimiter='-'; // the delimiter of date u can use - , / here depending on your date format

$datediff = get_datediff($date_delimiter, $date_end, $date_start); //call the fuction

$datediff variable will display the difference of the date in term of day.

Further, you can customize this function as per your requirement of date format ... cheers!!!!

No comments: