Page 1 of 1

Find Days Between 2 Dates Rate Topic: -----

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 28 May 2009 - 12:31 AM

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

explode
explode use for array to create an array of given value or string
example:
$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 want
string 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.
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

$sdate = "1/1/2009";
$edate = date('d/m/Y');
echo myDate("/",$sdate,$edate); //out put is 120



Is This A Good Question/Topic? 0
  • +

Page 1 of 1