我正在创建一个时钟应用程序。我已经设置好每个用户都有自己的表,它会选择该表和该表中的所有行。我想弄清楚如何在中间和下一个冲击之间取得区别。我假设每一拳都与下一拳(表格中的下一行(按ID排序)相对应)我只能想到约会。我知道情况就是这样,但我不知道如何实施。我是一个非常新的php开发人员(就在过去一周!)我不知道如何计算每个进出的差异。我看过这个问题:calculate the difference of the time between In and out但无法在那里或此处弄清楚:mysql timeclock。任何帮助表示赞赏。
我的确切问题是如何在表格中区分每个输入和输出。
文件:
<head>
<title>View My Punches</title>
<body bgcolor="#9966FF">
<link rel="icon" type="image/ico" href="http://example.com/time/favicon.ico"/>
</head>
<?php
error_reporting(E_ALL); ini_set('display_errors', 0);
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASSWORD', 'pass');
define('DB_HOST', 'host');
$link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($link->connect_errno > 0){
die('Could not connect: ' .connect_error());
}
$userid_value = $_POST['userid'];
$table = "tc_".$userid_value;
$checkusersql = "SELECT * FROM tc_users WHERE userid = '$userid_value'";
$usercheck = $link->query($checkusersql);
$punchessql = "SELECT * FROM $table ORDER BY id";
$result = $link->query($punchessql);
$unixtime = time() + 60*60;
$time_value = date("h:i:s A", $unixtime);
$date_value = date("m/d/Y", $unixtime);
if ($usercheck->num_rows == 0) {
echo "Sorry, " . $userid_value . " is not a valid user ID. Please try again.";
}else {
echo "Punch Report for " . $userid_value . " | Generated at " . $time_value . " on " . $date_value;
echo "<p></p>";
if ($result->num_rows == 0) {
echo "<p></p>";
echo "No punches were found for " . $userid_value . ".";
}else{
echo "<table border=1>";
echo "<tr><th>Punch ID</th><th>Time</th><th>Punch Type</th><th>Group</th><th>Department</th><th>Notes</th></tr>";
while ($row = $result->fetch_array())
{
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['time'] . "</td><td>" . $row['punchtype'] . "</td><td>" . $row['groupname'] . "</td><td>" . $row['dept'] . "</td><td>" . $row['notes'] . "</td>";
}
echo "</table>";
}
}
$differs = array();
$inout = array();
$current = array('in'=>array(),'out'=>array(),'length'=>'');
foreach ( $row as $each)
{
if ( $each['punchtype'] == 'in' )
{
if ( empty($current['in']) )
{ $current['in'] = $each; }
}
else if ( $each['punchtype'] == 'out' )
{
if ( empty($current['out']) )
{ $current['out'] = $each; }
}
if (( !empty($current['in']) && !empty($current['out'])))
{
$in = new DateTime($current['in']);
$out = new DateTime($current['out']);
$current['length'] = $in->diff($out);
$inout[] = $current;
$stamp = $inout['length'];
$stampformat = $stamp->format('%s');
$stampint = intval($stampformat);
$stampintval = $stampint/3600;
echo $stampintval;
#array_push($differs, );
}
}
?>
 
 
<form method="GET" action="http://example.com/time/panel.php">
<input type="submit" value="Go Home">
</form>
答案 0 :(得分:0)
在PHP中而不是在数据库中执行此操作会更简单。假设您已将所有记录拉入变量$ allofit,并且记录已按日期时间字段排序。现在你需要将它们配对成输出组。
$inout = array();
$current = array('in'=>array(),'out'=>array(),'length'=>'');
foreach ( $allofit as $each)
{
if ( $each['punchtype'] == 'in' )
{
if ( empty($current['in']) )
{ $current['in'] = $each; }
}
else if ( $each['punchtype'] == 'out' )
{
if ( empty($current['out']) )
{ $current['out'] = $each; }
}
if ( !empty($current['in']) && !empty($current['out'])
{
$in = new DateTime($current['in']);
$out = new DateTime($current['out']);
$current['length'] = $in->diff($out);
$inout[] = $current;
}
}
请注意,您当前的架构可能包含错误匹配的输入输出集。 (在@ 1:14,@ 1:15,out @ 1:40)这段代码将默默地丢弃不匹配;你应该做的就是尽可能确保首先不会发生不匹配。