This topic is locked

date subtracting

11/14/2010 9:36:54 AM
PHPRunner General questions
C
ColeSmith author

Help need
i ve 3 fields in my table Datecount. the fields are DateIn, DateOut and DaysSpent.

i want DaysSpent to calculate the difference between DateIn and DateOut.

Example: if i have 12/11/2010 for dateIn and 14/11/2010 will like the daysSpent to display 2dAYS

Sergey Kornilov admin 11/15/2010

If you use MySQL you can try DateDiff function:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
You can modify SQL Query the following way:

select

DateIn,

DateOut,

datediff(DateIn, DateOut) as DaysSpent

from ...


Note that DaysSpent is a calculated field which means you don't need a real DaysSpent field in the database.