Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:20:04+00:00 2026-06-10T23:20:04+00:00

I have a mysql log table of machine in the following format. ————————————————————————————————— |

  • 0

I have a mysql log table of machine in the following format.

     ---------------------------------------------------------------------------------------------------
    | Event_Id | Event_Type | Machine_No | Operator  | Time Stamp          | Shift | Reason     | Count |
     ---------------------------------------------------------------------------------------------------
    | 101      | Up         | Machine1   | operator1 | 2012-06-09 01:03:55 | S1    | Start      | 1     |
    | 102      | Up         | Machine2   | operator2 | 2012-06-09 01:03:55 | S1    | Start      | 1     |
    | 103      | Up         | Machine3   | operator3 | 2012-06-09 01:03:55 | S1    | Start      | 1     |
    | 104      | Down       | Machine1   | operator1 | 2012-06-09 02:03:55 | S1    | Break Down | 1     |
    | 101      | Up         | Machine1   | operator1 | 2012-06-09 02:33:55 | S1    | After BD   | 1     |
     ---------------------------------------------------------------------------------------------------

the table goes on like this.

by passing the following query.

$data = mysql_query("SELECT * FROM rpt_machine_log WHERE machine='machine1' AND shift='Shift1'")

i was able to get the following output.

 101    Up      machine1    operator1   2012-06-09 01:03:55 Shift1  Start of The Shift  1
 106    Down    machine1    operator1   2012-06-09 03:15:55 Shift1  Break               1
 109    Up      machine1    operator1   2012-06-09 03:30:55 Shift1  After The Break     1
 112    Down    machine1    operator1   2012-06-09 03:45:55 Shift1  Break Down          1
 115    Up      machine1    operator1   2012-06-09 05:00:55 Shift1  After Break Down    1
 116    Down    machine1    operator1   2012-06-09 05:30:55 Shift1  Break Down          2
 117    Up      machine1    operator1   2012-06-09 05:45:55 Shift1  After Break Down    2
 118    Down    machine1    operator1   2012-06-09 06:00:55 Shift1  End of Shift        1

Now i want to find the difference of each consecutive Up and Down Time in php code.

I also want to add shift2 to same query to display machine1 log for both shift 1 & 2.

since i’m new to php i was not able to solve this.

Can any help me out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:20:06+00:00Added an answer on June 10, 2026 at 11:20 pm

    To get shift2 aswell I’d do:

    SELECT * 
    FROM rpt_machine_log 
    WHERE machine='machine1' AND (shift='Shift1' OR shift='Shift2')
    ORDER BY shift,`Time Stamp`
    

    With columns that have spaces or special characters you want to add ` around them. To be honest it is safer to just do this for all columns. Some columns, such as Add would cause a query to fail since add is a reserved keyword in sql.

    If you can guarantee that up will always follow down then you could loop through your result:
    while($row=mysql_fetch_assoc($data)){}

    After this there are plenty of ways to go to store the data, could try:

    if(isset($timestamp)){
    $difftime=strtotime($row['Time Stamp'])-strtotime($timestamp);
    $hours=floor($difftime/3600);
    $difftime-=$hours*3600;
    $minutes=floor($difftime/60);
    $difftime-=$minutes*60;
    $seconds=$difftime;
    $diff_array[]=$hours.":".$minutes.":".$seconds;
    unset($timestamp);}
    else{
    $timestamp=$row['Time Stamp'];}
    

    And there you have an array of all the time differences.

    Obviously if you can’t guarantee that up will always come after down, or you want to split up the time diffs for shift1 and shift2 then you will have to add in extra checks to the while loop.

    —EDIT—

    if(isset($timestamp)){
    $difftime=strtotime($row['Time Stamp'])-strtotime($timestamp);
    $diff_array[]=$difftime;
    unset($timestamp);}
    else{
    $timestamp=$row['Time Stamp'];}
    
    foreach ($diff_array as &$value){
    $Uptime += $value;}
    
    $hours=floor($Uptime/3600);
    $Uptime-=$hours*3600;
    $minutes=floor($Uptime/60);
    $Uptime-=$minutes*60;
    $seconds=$convert;
    $Uptime=$hours.":".$minutes.":".$seconds;
    

    —EDIT2—

    while($row=mysql_fetch_assoc($data)){
    $array['event_type'][]=$row['event_type'];
    $array['timestamp'][]=$row['Time Stamp'];}
    

    Now you have an array of two arrays: event_types and timestamps. You could just do this as two distinct arrays too if you want e.g. $event_type[]=$row['event_type']; and change the rest accordingly.

    You could now do a for loop to iterate over these results and do the checks you need.

    $count=count($array['event_type']);
    for($x=0;$x<$count;$x++){
    if($row['event_type'][$x]=='Down' && $row['event_type'][$x+1]=='Up'){
    }}
    

    Remember to calculate the count before the for loop, putting it in as one of the conditions means it would be calculated every time and therefore has a performance cost.

    Also if you want to skip the first result of your mysql results just call $row=mysql_fetch_assoc($data) once before the while loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql trigger that logs every time a specific table is updated.
Consider following MySQL table: CREATE TABLE `log` ( `what` enum('add', 'edit', 'remove') CHARACTER SET
We have a table on mysql to log all visitors of our site. The
I have a MySQL table with 5 rows: email message_id date time And I
I have this log table in MySQL with columns ActionName and SourceName. The same
I have a mysql table called users with the following fields: username - password
I have a database table for log messages and at any time there can
I have a mysql table with a row called log. I need to update
I am using mysql and have a table (Log_Table) storing user login dates, a
I have a MySQL table with the structure: beverages_log(id, users_id, beverages_id, timestamp) I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.