I have a database table build in this structure.
ID - INT
addDate - DateTime
sunrise - Time
sunset - Time
Now I'm trying to retrieve the difference in minutes between
SELECT addDate, TIMESTAMPDIFF(MINUTE, sunrise, sunset) AS duration FROM SunData WHERE date(addDate) BETWEEN curdate() - interval 7 day and curdate() ORDER BY addDate ASC
The results I'm getting NULL in every row for duration.
Code to display these values
$date = date("Y-m-d H:i:s", strtotime(str_replace('-','/',$row->addDate)));
$duration = $row->duration / 60;
echo "Dauer: " . $duration . "Stunden
";
Why are the values getting less and less? Is my query correct?
You are ordering by addDate
in the ascending order. You are taking the difference between now()
and addDate
. Ascending order means addDate
keeps getting bigger, so the difference between addDate
and now()
keeps getting smaller.
I don't know exactly what your goal is- but with each record you getting closer to now()- that's why the values are getting less and less.
Your query don't use the sunrise and sunset fields:
SELECT addDate, TIMESTAMPDIFF(MINUTE, DATE_ADD(addDate,sunset), DATE_ADD(addDate, sunrise))