Calculate difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Calculate difference

Post by sandy1028 »

Code: Select all

         0 | 2008-06-10 15:30:01 | A
           1 | 2008-06-10 15:40:01 | A  // Calcute timestamp as 10 min
           1 | 2008-06-10 15:50:01 |A //Skip
           1 | 2008-06-10 16:00:01 |A  //Skip
           1 | 2008-06-10 16:10:01 |A   //Skip
           0 | 2008-06-10 16:20:01 |A   // Calcute timestamp as 10 min
           1 | 2008-06-10 16:30:01 |A  
           1 | 2008-06-10 16:40:01 |A  //Skip
           1 | 2008-06-10 16:50:01 |A  //Skip
           1 | 2008-06-10 17:00:01 |A  //Skip
           0 | 2008-06-10 17:10:01 |A   
           1 | 2008-06-10 17:20:01 |A   //Calcute difference as 10 min
           1 | 2008-06-10 17:30:01 |A   
           0 | 2008-06-10 17:40:01 |A
           0 | 2008-06-10 17:50:01 |A //Clauculate as 20 min
and how to push into array as it shows like this in the table

The fields are Status, imestamp and Name.
As in the above example
It should display In the table the

Name| Start Time | End Time | Difference

A | 2008-06-10 15:30:01 |2008-06-10 15:40:01 | 10
A | 2008-06-10 16:10:01 | 2008-06-10 16:20:01 | 10


Based on the status 0 timestamp difference has to be calculated and if status is 1 then skip the row.
I


Please help me.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Calculate difference

Post by hansford »

Code: Select all

 
<?php
 
while($row = mysql_fetch_array($result)){
 
if($row['status'] == 1) { continue; }
 
echo $row['whatever'];
 
}
 
?>
 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Calculate difference

Post by superdezign »

sandy1028 wrote:

Code: Select all

         0 | 2008-06-10 15:30:01 | A
           1 | 2008-06-10 15:40:01 | A  // Calcute timestamp as 10 min
[...]

Code: Select all

          1 | 2008-06-10 16:10:01 |A   //Skip
           0 | 2008-06-10 16:20:01 |A   // Calcute timestamp as 10 min
[...]

Code: Select all

          0 | 2008-06-10 17:10:01 |A   
           1 | 2008-06-10 17:20:01 |A   //Calcute difference as 10 min
[...]

Code: Select all

          0 | 2008-06-10 17:40:01 |A
           0 | 2008-06-10 17:50:01 |A //Clauculate as 20 min
sandy1028 wrote:Based on the status 0 timestamp difference has to be calculated and if status is 1 then skip the row.
Your logic for calculation is inconsistent. The first calculation is between a row with status 0 and one with 1. Then, you skip rows with status 1, and your next calculation is between a row with status 1 and one with status 0. Then you skip the rows with status 1, and make the next calculation between a row with stats 0 and a row with status 1, just like the first calculation. And the last calculation between a row with status 0 and another with status 0 make this completely inconsistent.

You're going to have to give better logic or more insight into the meaning of this data if you want any help.
Post Reply