Reputation: 28294
Editing the question.
I have SQL like this:
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY
Now 24 hours make a whole day. However, what if I want to do the query for the last 3 hours or so?
My table1
.DateField
is in the format of 2010-03-10 10:05:50
.
Original post:
If I have this
1 hour
2 hours
3 hours
..
24 hours
How would I change it to days?
Thanks.
Upvotes: 1
Views: 7336
Reputation: 96159
MySQL not only knows DAY
as a unit for an interval but also HOUR
, MINUTE
, ....
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
$x = 32;
$sql = "SELECT
x,y,z
FROM
foo
WHERE
`table1`.`DateField` >= NOW() - INTERVAL $x HOUR
";
Upvotes: 1
Reputation: 7712
$hours = 80;
$hid = 24; // Hours in a day - could be 24, 8, etc
$days = round($hours/$hid);
if( $days < 0 )
{
echo "$hours hours";
}
else
{
echo "$days days";
}
This assumes you want the hours if it's less than 1 day. If not just remove the switch.
Upvotes: 6
Reputation: 8169
As simple as:
if you want to convert the total of those hour to day: Just sum the total of hours and that total must be divided by 24
(1 + 2 + 3 + 5) / 24
If you want to convert all of those hours to days: Just divide by 24 every hours in your list
(1/24) (2/24) (3/24) (5/24)
Upvotes: 1