Solarpitch Posted January 13, 2009 Share Posted January 13, 2009 Hi, I need to create an array to hold a series of hours of the day. I want each array value to hold a start and finish time. So for example... $array['hours'] = array ($start => '6:00', $finish => '7:00') $array['hours'] = array ($start => '8:00', $finish => '9:00') $array['hours'] = array ($start => '9:00', $finish => '10:00') Is something like this possible? Becasue I then want to loop through the array for each value and run this query: function hourly_breakdown($date, $start, $finish) { $query = $this->client->query('SELECT SUM(price) as hour_total FROM golfpro_barsales WHERE date = '.$date.' AND time > '.$start.' AND time < '.$finish.' GROUP BY `price`'); $row = $query->row(); return $row->hour_total; } This is so I can get a breakdown of how much was sold for each hour in the system. So effectively I will then format it like: 6:00 - 7:00: Total €245.00 7:00 - 8:00: Total €124.00 .. etc Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/ Share on other sites More sharing options...
uniflare Posted January 13, 2009 Share Posted January 13, 2009 to me it only seems effective if you use unix timestamps. time() will give you the current timestamp, date("d/m/y H:i:s",$timestamp) will give you the date & time of that specific timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736066 Share on other sites More sharing options...
xtopolis Posted January 13, 2009 Share Posted January 13, 2009 Actually, you could probably accomplish this with just a mysql query... http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour It's probably very similar to this thread: http://www.phpfreaks.com/forums/index.php/topic,233831.0.html Otherwise: $hours = array( array ("start" => '6:00', "finish" => '7:00'), array ("start" => '8:00', "finish" => '9:00') ) Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736067 Share on other sites More sharing options...
Solarpitch Posted January 13, 2009 Author Share Posted January 13, 2009 Thanks for that lads, @xtopolis ... could probably use the hour format thing, but for example... if I got the value from 8:00 - 9:00.. then next value would need to be 9:01 - 10:00 so it might not work. In the example you shown, how would I loop through this array then? I might be way off here but would it be like... <?php foreach($hours as $e) { $start = $e->start; $finish = $e->finish; $query = $this->client->query('SELECT SUM(price) as hour_total FROM golfpro_barsales WHERE date = '.$date.' AND time > '.$start.' AND time < '.$finish.' GROUP BY `price`'); $row = $query->row(); return $row->hour_total; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736072 Share on other sites More sharing options...
killah Posted January 13, 2009 Share Posted January 13, 2009 You need to set variable's. So you could use something like this similar to the array's. <?php $hours = array ( 1 => array ( 'start' => '6:00', 'finish' => '7:00', ), 2 => array ( 'start' => '8:00', 'finish' => '9:00', ) ); $rows = count($hours); for ($i = 1; $i <= $rows; $i++) { $query = $this->client->query('SELECT SUM(`price`) AS `hour_total` FROM `golfpro_barsales` WHERE `date`='.$date.' AND `time` > '.$hours[$i]['start'].' AND `time` < '.$hours[$i]['finish'].' GROUP BY `price`'); $row = $query->row(); echo $row->hour_total.'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736121 Share on other sites More sharing options...
Solarpitch Posted January 13, 2009 Author Share Posted January 13, 2009 Hi Thanks for that, I came up with a simpler solution... <?php $hours = array( array ("start" => '14:00:00', "finish" => '15:00:00'), array ("start" => '15:00:00', "finish" => '16:00:00') ); foreach($hours as $e) { $start = $e['start']; $finish = $e['finish']; $hour_total = $this->report_model->hourly_breakdown($date, $start, $finish); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736129 Share on other sites More sharing options...
redarrow Posted January 13, 2009 Share Posted January 13, 2009 can one off you show me this as a array code please as a written example i tried and got no where please let me see it as a written example cheers. want to test it in my browser. Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736134 Share on other sites More sharing options...
killah Posted January 13, 2009 Share Posted January 13, 2009 As for mine you can use this: <?php $hours = array ( 1 => array ( 'start' => '6:00', 'finish' => '7:00', ), 2 => array ( 'start' => '8:00', 'finish' => '9:00', ) ); echo '<pre>'; print_r($hours); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736136 Share on other sites More sharing options...
sasa Posted January 13, 2009 Share Posted January 13, 2009 'SELECT SUM(price) as hour_total, LEFT(`time`, 2) as h FROM golfpro_barsales WHERE date = '.$date.' GROUP BY LEFT(`time`,2)' Quote Link to comment https://forums.phpfreaks.com/topic/140648-help-needed-with-array/#findComment-736137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.