# Getting days from dates

• May 8, 2005, 00:41
Tapan
Getting days from dates
Hello,

I have 2 dates lets say: 1-1-2005 and 5-1-2005. Now there are 5 days in those dates. I want to get the days from the 2 specified days.

Thanks.
• May 8, 2005, 01:32
elel1138
If I understood correctly, you want to find how many days fit between two dates? Use the timestamp function- it converts any date to "number of seconds from January 1, 1970"; check the manual for mktime() function.

PHP Code:

```  //\$date1, \$date2 - dates to operate on   \$d1 = date(\$date1,"d");   \$m1 = date(\$date1,"m");   \$y1 = date(\$date1,"y");   // the same with date2   //now, convert the dates to timestamps:   \$ts1 = mktime(0,0,0,\$d1, \$m1, \$y1);   \$ts2 = mktime(0,0,0,\$d2, \$m2, \$y2);   //substract the two timestamps; use the abs function to avoid negative numbers   \$d = abs(\$ts1-\$ts2);   //divide by the number of seconds in one day   \$days = \$d/86400;   //et voila!  ```
• May 8, 2005, 01:37
killerkooki
PHP Code:

``` \$sVar_dateString1 = '1-1-2005'; \$sVar_dateString2 = '5-1-2005'; \$aVar_date1 = explode('-', \$sVar_dateString1); \$aVar_date2 = explode('-', \$sVar_dateString2); \$iVar_time1 = mktime(0,0,0,\$aVar_date1[1],\$aVar_date1[0], \$aVar_date1[2]); \$iVar_time2 = mktime(0,0,0,\$aVar_date2[1],\$aVar_date2[0], \$aVar_date2[2]); \$iVar_timeDifference = \$iVar_time2 - \$iVar_time1; \$iVar_dayDifference = (\$iVar_timeDifference / (60 * 60 * 24)) + 1; echo \$iVar_dayDifference;  ```
The +1 is there since it seemed that you wanted it with both days inclusive.
If you want the days between, excluding the boundry dates, make it - 1 instead.
Hope that helps. :)
• May 8, 2005, 01:47
elel1138
I was faster :) - however, the note about +1 is important, I somehow forgot to write it.
• May 8, 2005, 01:54
killerkooki
Hehe... yeah, I only saw your post after I posted. :)
• May 8, 2005, 02:04
Tapan
Hi,

Thanks for the help. But isn't this solution good enough if it works ? I was trying this query but it does not seems to be working and i am getting an error instead:

PHP Code:

``` \$q3 = "SELECT DATEDIFF ('" . \$a1["sdate"] . " 23:59:59', '" . \$a1["fdate"] . "')";                 \$r3 = mysql_query(\$q3) or die(mysql_error());                 echo mysql_result(\$r3, 0);  ```

The error i am getting is:

Quote:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '('2005-05-10 23:59:59', '2005-06-18')' at line 1

Thanks
• May 8, 2005, 02:18
killerkooki
The manual says that the DATEDIFF function was added only in version 4.1.1.
So quite likely, the error is because your version is older.
• May 8, 2005, 02:23
Tapan
Hi,

How do i come to know which version i am using ?
http://www.avissoftware.com/phpinfo.php will this help identify the version ?

Thanks
• May 8, 2005, 02:29
killerkooki
Take a looksie here.
:)
• May 8, 2005, 02:48
Tapan
Hi,

Thanks silly me.

Thanks again!
• May 8, 2005, 02:58
shaman
hey, look to strtotime() too
• May 8, 2005, 03:45
elel1138
Using the database is not a good solution. Why use a database (create connection, execute query, drop connection) and waste resources when you can do the same with 5 lines of code? It's against Occam's razor :)