# Thread: how to find the difference b/w two dates

1. ## how to find the difference b/w two dates

hi

how to find the difference b/w two dates in php.

the results of date(...) have to be compared and I'd like to know the difference in terms of dates.

Thanks

2. That depends on what format you get the date in. If you get it in something like this: 2007-03-17 then you need to convert it to a unix date with the strtotime() function.

Once that is done you will have the dates as number of seconds since January 1 1970 00:00:00 GMT

You can then compare the two dates and figure out how many seconds there are between the two dates.

Then it is simple math.

\$mins_between_dates = \$secs_between_dates/60;
\$hours_between_dates = \$secs_between_dates/3600;
\$days_between_dates = \$secs_between_dates/86400;

3. that would depend somewhat on what kind of timestamp you have? epoch? mysql?

4. http://www.php.net/manual/en/function.date.php
Code:
```function DatesBetween(\$startDate, \$endDate){
// get the number of days between the two given dates.
\$days = (strtotime(\$endDate) - strtotime(\$startDate)) / 86400 + 1;
\$startMonth = date("m", strtotime(\$startDate));
\$startDay = date("d", strtotime(\$startDate));
\$startYear = date("Y", strtotime(\$startDate));
\$dates;//the array of dates to be passed back
for(\$i=0; \$i<\$days; \$i++){
\$dates[\$i] = date("n/j/Y", mktime(0, 0, 0, \$startMonth , (\$startDay+\$i), \$startYear));
}
return \$dates;
}

example: \$dates = DatesBetween("2/27/2007","3/3/2007")

Array ( [0] => 2/27/2007 [1] => 2/28/2007 [2] => 3/1/2007 [3] => 3/2/2007 [4] => 3/3/2007 )```

5. mysql

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•