获取总行数小时和分钟

时间:2016-07-30 10:39:42

标签: php mysql time

因此,我以下列方式获得用户的总登录时间。现在我希望获得time_in和time_out datetime字段的总计数,并显示所有总小时和分钟。

$query = "
  SELECT member_id
       , member_name
       , team
       , time_in
       , time_out
       , SEC_TO_TIME(UNIX_TIMESTAMP(time_out) - UNIX_TIMESTAMP(time_in)) totalhours 
    FROM hours 
   WHERE member_id = 7;
   ";

$result = mysql_query($query)or die(mysql_error());
$rowNo = 1; //Increment Row Number
while($row = mysql_fetch_assoc($result)){
$time = $row['totalhours'];

echo "<tr align='left'>";    
echo"<td><font color='white'>" .$rowNo++."</font>.</td>";
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>";
echo"<td><font color='white'>" .$time." Hrs</font>.</td>"; 
echo "</tr>";          
}

这是图像输出

Output of current query

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

使用此代码,我希望这有帮助

$query = "select member_id, member_name, team, time_in, time_out, sec_to_time(unix_timestamp(time_out) - unix_timestamp(time_in)) AS totalhours from hours WHERE member_id ='7'";

$result = mysql_query($query)or die(mysql_error());
$rowNo = 1; //Increment Row Number
$total_time="00:00:00";
while($row = mysql_fetch_assoc($result)){
$time = $row['totalhours'];

$secs = strtotime($time)-strtotime("00:00:00");
$total_time = date("H:i:s",strtotime($total_time)+$secs);


echo "<tr align='left'>";    
echo"<td><font color='white'>" .$rowNo++."</font>.</td>";
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>";
echo"<td><font color='white'>" .$time." Hrs</font>.</td>"; 
echo "</tr>";          
}

echo $total_time;

答案 1 :(得分:0)

PHP mysql_num_rows($ result)中有一个函数,它将返回mysql_query函数返回的行数。

答案 2 :(得分:-1)

首先,您需要将mysql_ *更改为mysqli_ *或pdo。

count()就是你的朋友。

你的查询应该是这样的。

select count(*) as total,member_id, member_name, team, time_in, time_out, sec_to_time(unix_timestamp(time_out) - unix_timestamp(time_in)) AS totalhours from hours WHERE member_id ='7'

现在,如果您回复$row['total'],您将获得记录的总数。

  

更新

保持查询不变,并创建$ total_rows变量以保存总行数。

$ total_rows = mysql_num_rows($ result);

echo "<tr align='left'>";    
echo"<td><font color='white'>" .$rowNo++."</font>.</td>";
echo"<td><font color='white'>" .$total_rows."</font>.</td>";
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>";
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>";
echo"<td><font color='white'>" .$time." Hrs</font>.</td>"; 
echo "</tr>";