Day Different Between 2 Dates
one of the problems that i was facing was time and date difference in PHP and saw here are many users that they want date difference between 2 date so in tutorial i'm going to show how to get days between 2 date.
i am using PHP 5+ and the following function please read these function before reading this tutorial.
gregoriantojd Parameters:
month Month of the Year
day Day of the Month
year Year
explodeexplode use for array to create an array of given value or string
example:
CODE
$explod = explode(" ","this is a sample text");
echo $explod[3];//our put sample
//or we can use an loop for printing value of array
separator a string separator value exmaple
"/" or "-" or " " or any thing you wantstring the string value to search and cut by separator value
limit optional maximum length
ok lets start:
here i am changing the date format for
gregoriantojd, because i'm using "dd-mm-yyyy" format in my example and if we explode it, the value of array changing to (0) for day, (1) for month, (2) for year and gregoriantojd is asking first for month, day and year.
first create a function.
CODE
function MyDate($explod,$startDate,$endDate)
{
$d1 = explode($explod,$startDate);
$d12 = gregoriantojd($d1[1],$d1[0],$d1[2]);
$d2 = explode($explod,$endDate);
$d22 = gregoriantojd($d2[1],$d1[0],$d1[2]);
$diff = $d22 - $d12;
return $diff;
}
how to use it
CODE
$sdate = "1/1/2009";
$edate = date('d/m/Y');
echo myDate("/",$sdate,$edate); //out put is 120