Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The idea is to use <a href="http://dev.mysql.com/doc/refman/5.0/fr/date-and-time-functions.html#idp52185520" rel="nofollow"><code>TIMESTAMPDIFF</code></a> that will substract the <code>end_date</code> with the <code>start_date</code> :</p> <pre><code>$stoppage_part_sum[] = MouldingStoppage::find( array( 'select' =&gt; 'SUM( ABS(TIMESTAMPDIFF( MINUTE, IF(end_date &gt; \''.date('Y-m-d H:00', $j).'\', \''.date('Y-m-d H:00', $j).'\', end_date), IF(start_date &lt; \''.date('Y-m-d H:00', $i).'\', \''.date('Y-m-d H:00', $i).'\', start_date) )) ) as stoppage_qty', 'conditions' =&gt; array( 'moulding_product_id IN (?) AND end_date &gt;= ? AND start_date &lt;= ?', $this-&gt;product_id, date('Y-m-d H:00', $i), date('Y-m-d H:00', $j) ) ) )-&gt;stoppage_qty; </code></pre> <p>The <code>IF(end_date &gt; ...)</code> and <code>IF(start_date &lt; ...)</code>is to take only the time into the current window. It will then use this :</p> <pre><code>start_date: 08:00 end_date: 08:15 start_date: 08:19 end_date: 08:38 start_date: 08:45 end_date: 09:00 </code></pre> <p>Then you just <code>TIMESTAMPDIFF</code> <code>end_date</code> and <code>start_date</code> and <code>SUM</code> them. </p> <p>Note the condition to match the concerned "stoppages". (<code>start_date</code> &lt; <strong>j</strong> and <code>end_date</code> > <strong>i</strong>)</p> <p><strong><a href="http://sqlfiddle.com/#!2/7f7825/11" rel="nofollow">The demo sqlFiddle</a></strong></p> <p>Try to change the dates in data to watch the result. </p>
 

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