rashmi_k28 Posted April 9, 2008 Share Posted April 9, 2008 Hi, I have a code to find the timestamp when the status changes from 0 to 1. But my problem is when the status doesnot change to 1 I have to calculate the time from first timestamp to now(). Please help me $sql = "SELECT status,timestamp,name FROM table WHERE name='".$res[0]."' and date(timestamp)='".$res2[0]."'"; $row = mysql_query($sql); $previousStatus = ''; $currentStatus = ''; $previoustime=''; $currentTime=''; while($result = mysql_fetch_array($row)) { $sql_ip="select ifnull(ip,'--') from table1 where name='".$res[0]."'"; $sql_ip1=mysql_query($sql_ip); while($ip=mysql_fetch_array($sql_ip1)){ if($ip[0]==""){ $ip[0]='--'; } $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]#$previoustime#$result[1]#$d"); $previousStatus = $currentStatus; $previoustime=$currenttime; } Link to comment https://forums.phpfreaks.com/topic/100301-calculate-time/ Share on other sites More sharing options...
rashmi_k28 Posted April 10, 2008 Author Share Posted April 10, 2008 This is the rows from the database. the fields are name, timestamp , status. |hardware| 2008-04-10 08:30:01 | 0 | | hardware| 2008-04-10 08:40:01 | 0 | | hardware| 2008-04-10 08:50:02 | 0 | |hardware | 2008-04-10 09:00:01 | 0 | | hardware | 2008-04-10 09:10:01 | 0 | | hardware | 2008-04-10 09:20:01 | 0 | | hardware | 2008-04-10 09:30:01 | 0 | | hardware | 2008-04-10 09:40:02 | 0 | | hardware| 2008-04-10 09:50:01 | 0 | | hardware | 2008-04-10 10:00:01 | 0 | | hardware| 2008-04-10 10:10:01 | 0 | | hardware| 2008-04-10 10:20:01 | 0 | | hardware| 2008-04-10 10:30:01 | 0 | | hardware| 2008-04-10 10:40:01 | 0 | | hardware | 2008-04-10 10:50:01 | 0 | | hardware | 2008-04-10 11:00:01 | 0 | | hardware| 2008-04-10 11:10:01 | 0 | |hardware | 2008-04-10 11:20:01 | 0 | | hardware| 2008-04-10 11:30:01 | 0 | | hardware| 2008-04-10 11:40:01 | 1 | |hardware| 2008-04-10 11:50:01 | 1 | | hardware| 2008-04-10 12:00:01 | 1 | $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]#$previoustime#$result[1]#$d"); $previousStatus = $currentStatus; $previoustime=$currenttime; } } This is the code when I use this the code only last timestamp '2008-04-10 11:30:01' is taken not '2008-04-10 08:30:01'. Please anyone tell me how to traverse and find the timestamp 2008-04-10 08:30:01. Link to comment https://forums.phpfreaks.com/topic/100301-calculate-time/#findComment-513696 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.