tags:

views:

68

answers:

3

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.

+1  A: 

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)

Garis Suero
+1  A: 
$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.

Marco Ceppi
please see edited question
+1  A: 

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
";
VolkerK