Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Array with date/time and duration for highchart
    primarykey
    data
    text
    <p>So first here's Fiddle of what I am trying to achieve in the end: <a href="http://jsfiddle.net/hQXvR/" rel="nofollow noreferrer">Fiddle</a></p> <p>To do so I need to get values from three different tables: ok_part, reject and stoppage.</p> <p>As for stoppage, the table structure looks like this:</p> <p><img src="https://i.stack.imgur.com/J0eZZ.jpg" alt="Stoppage table structure"></p> <p>The x Axis of my chart are one hour gaps, the first x Axis is always time 8 hours ago and the last one time from now to next full hour. </p> <p>I've managed to put together ok_part and reject arrays for highchart.</p> <pre><code>class GraphCalc() { ..... /* calculate time */ function CalculateTime($diff, $form) { $new = new DateTime(); $new-&gt;modify($diff); return $form != 0 ? $new-&gt;format('Y-m-d H:00') : $new-&gt;format('Y-m-d'); } function GraphInterval() { $time = strtotime($this-&gt;CalculateTime('-8 hours', 1)); // calling function to get time 8 hours ago $now = strtotime(date('Y-m-d H:00')); /* looping through time (8 hours ago to now) */ for ($i = $time, $j = $i + 3600; $i &lt;= $now; $i += 3600, $j += 3600) { /* puts together array for x Axis */ $this-&gt;xAxis_array[] = "'".date('H:00', $i).' - '.date('H:00', $j)."'"; /* queries to sum ok parts and rejects according to time */ $ok_part_sum[] = MouldingPart::find( array( 'select' =&gt; 'SUM(amount) as ok_qty', 'conditions' =&gt; array( 'moulding_product_id IN (?) AND date &gt;= ? AND date &lt;= ?', $this-&gt;product_id, date('Y-m-d H:00', $i), date('Y-m-d H:00', $j) ) ) )-&gt;ok_qty; $reject_part_sum[] = MouldingReject::find( array( 'select' =&gt; 'SUM(amount) as reject_qty', 'conditions' =&gt; array( 'moulding_product_id IN (?) AND date &gt;= ? AND date &lt;= ?', $this-&gt;product_id, date('Y-m-d H:00', $i), date('Y-m-d H:00', $j) ) ) )-&gt;reject_qty; } /* looping through ok_part and reject arrays to add 0 where there's no value */ for ($i = 0; $i &lt; count($ok_part_sum); $i++) { $this-&gt;chart_ok_array[] = $ok_part_sum[$i] == '' ? 0 : $ok_part_sum[$i]; $this-&gt;chart_reject_array[] = $reject_part_sum[$i] == '' ? 0 : $reject_part_sum[$i]; } } function xAxis() { /* return x Axis values */ return implode(', ',$this-&gt;xAxis_array); } function yAxisValues() { /* put together arrays for highchart */ $chart_data[] = "{data: [".implode(", ",$this-&gt;chart_ok_array)."], name: 'OK', color: '#89A54E'}"; $chart_data[] = "{data: [".implode(", ",$this-&gt;chart_reject_array)."], name: 'PRAAGID', color: '#AA4643'}"; return $chart_data; } } </code></pre> <p>I've would like help to get a solution for stoppage data. Stoppage has two dates that need to be considered: start_date and end_date. Based on the x Axis and stoppage dates I need to calculate the duration. Sometimes I could probably use the duration field, but I think it is easier to avoid it and just calculate it on the fly.</p> <p>Here's an example of what I would like to achieve with stoppage:</p> <p>Let's say x Axis is 05:00 - 06:00 then stoppage duration should be 59 minutes, because there is an entry in stoppage table with start_date 2013-09-05 05:01:00 and end_date 2013-09-05 09:00:00, but I can't use the end date of stoppage table value, because I need to use the x axis value which is 06:00.</p> <p>I hope you get an idea of what I am trying to achieve, it is really hard to explain it. </p> <p>The data in stoppage table might vary as well. In one hour there might be multiple stoppages. Let's say x Axis is 08:00 - 09:00 and in table there are three stoppages:</p> <p>start_date: 07:54 end_date: 08:15<br> start_date: 08:19 end_date: 08:38<br> start_date: 08:45 end_date: 09:47<br> So the duration from 08:00 - 09:00 should be 49 minutes.</p> <p>Please help me with this, I've been stuck with this for days now.. I tried to explain it as good as I could, but I bet it's still confusing.</p> <p>In the end I would like to get an array for highchart, like I have with ok_part and reject.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload