# Thread: Getting days from dates

1. ## 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.

2. 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!  ```

3. 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.

4. I was faster - however, the note about +1 is important, I somehow forgot to write it.

5. Hehe... yeah, I only saw your post after I posted.

6. 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:

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

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.

8. 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

9. Take a looksie here.

10. Hi,

Thanks silly me.

Thanks again!

11. hey, look to strtotime() too

12. 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

#### Posting Permissions

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