1

I have this table in mysql

Date one   | Date tow   | 
2012-05-20 | 2012-05-04 | = 16 days 
2012-05-12 | 2012-05-08 | = 4 days
                          = 20 days

and i want to select difference between two dates and then sum all days.

Robert Coroianu
  • 379
  • 2
  • 7
  • 15
  • Try this http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php – Bharu Jul 11 '13 at 07:44

4 Answers4

2

If you should do it in MySQL, you could use DATEDIFF function.

SELECT DATEDIFF(dateone, datetwo) AS d FROM tablename

and then you could aggregate this result the way you want, example

SELECT SUM(DATEDIFF(dateone, datetwo)) AS s FROM tablename

You can do it also in PHP after fetching the dates

Nedret Recep
  • 726
  • 5
  • 8
0
SELECT SUM(DATEDIFF(`Date one`, `Date tow`)) FROM `Table`

See DATEDIFF() and SUM().

Note that in MySQL, you do not need to use GROUP BY in order to be able to use the aggregate function SUM(). In that case, MySQL will regard all rows as a single group.

Oswald
  • 31,254
  • 3
  • 43
  • 68
0

You can try the below query. hope it will sort out the issue.

SELECT SUM(DATEDIFF(DATE1,DATE2)), FROM TABLE 
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0

You can use the DATEDIFF function in mysql.

SET @runtot:=0;
SELECT DATEDIFF(one,tow) AS diff, (@runtot := @runtot + DATEDIFF(one,tow)) AS runningsum
FROM table 
DevZer0
  • 13,433
  • 7
  • 27
  • 51