Jump to content

Store a variable based on loop


rashmi_k28

Recommended Posts

Hi,

 

The fields of a table are

status,timestamp,name.

 

Status is 0 or 1

Timestamp is increment of 10 minutes.

 

I have to calculate the timestamp from when status is from 0 to 1.

 

| 0 | 2008-05-29 14:10:02 |A

| 0 | 2008-05-29 14:20:02 |A

| 1 | 2008-05-29 14:30:02 |A

| 0 | 2008-05-29 14:40:02 |A

| 1 | 2008-05-29 14:50:02 |A

 

Here at 2008-05-29 14:10:02 status was 0 and at 2008-05-29 14:30:02 status was 1. So I have to calculate the difference between two. And again from 2008-05-29 14:40:02 to 2008-05-29 14:50:02

 

 

When I use the below code I get time difference as 20 min and 20 min.

 

 

I  have used the code as

 

$currentStatus = $result['status'];
    $currentTime = $result['timestamp'];
    $previousStatus = $previousStatus == '' ? $currentStatus : $previousStatus;
    $previoustime = $previoustime == '' ? $currentTime : $previoustime;

    if($previousStatus == $currentStatus)
    {
                $previousStatus=$currentStatus;
  //              $previoustime=$currentTime;
                continue;
                }
        $d = strtotime($currentTime) - strtotime($previoustime);
        //array_push($records,"$result[2]#$ip[0]#$result[2]#$result[1]#$previoustime#$d");
         array_push($records,"$result[2]#$ip[0]#$result[2]#$previoustime#$result[1]#$d");


    $previousStatus = $currentStatus;
        $previoustime=$currentTime;

 

But the calculation is not proper. The previoustime is creating problem. It is taking the currenttime of previous row as the next previoustime.

 

 

And if status is 0 through out the day then how to add the condition and calculate time differnce from first row and last row.

 

 

Please help me how to solve this problem

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.