Mohammad Alfares
Mohammad Alfares

Reputation: 33

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

Upvotes: 1

Views: 189

Answers (2)

Mohammad Alfares
Mohammad Alfares

Reputation: 33

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

Upvotes: 0

Alexander Jardim
Alexander Jardim

Reputation: 2476

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

Upvotes: 1

Related Questions