简体   繁体   中英

PHP Split array values based on time

I want this human readable words into php code:

Take all values from column "Ratings" WHERE Time >= 24 hours Split them into 1 hour each Take average Rating values for each hour and put them into 24 variables

WhatI'm doing now is a bad long code :/

I'm taking them an hour after another, each one in a separate function where I specify an hour via mysql select statement!

I'm new in programming, and I couldn't figure out what to do to get all values at once, and split them into 1 hour each

Thanks

EDIT : changed the fiddle, so it uses time as an int, that represents seconds since January 1st, 1970.

Take a look at this fiddle: http://sqlfiddle.com/#!2/6e6b6/5/0 It is returning the rating average on a hourly basis. Is this that you need? I think you might tune the query for your other needs.

You can read more about it at http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html

Got it, after so many searches. Learning is beautiful :)

This is the code

$mysql = "select date_format(from_unixtime(Time), '%H') as hour, avg(Rate) from za7ma1 group by hour";

$result = mysql_query($mysql) or die(mysql_error());

            // Print out result
    while($row = mysql_fetch_array($result)){
        echo "The average for this hour ". $row['hour']. " is ".$row['avg(Rate)'];
        echo "<br />";
    }

Thanks to Alexander Jardim for the great tool http://sqlfiddle.com/#!2/6e6b6/5/0 which opened my eyes on avg() mysql function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM