获取MySQL中3之后发生的所有1

时间:2015-06-16 08:46:31

标签: php mysql

我有一张表作为打卡时钟,用户进出。

出 - 0 在 - 1 午餐 - 2 Out On Business - 3

如果有人出差,然后重新登录,则会发出3然后再签1回来。

在查看一周内的总小时数时,它会查找所有内容和小时,但由于在3之后有一个额外的1,它会跳过第一个小时。

有没有人对如何做到这一点有任何想法?

由于

function getUsersHours($fromweek, $toweek, $userId)
{

if($fromweek === $toweek)
{
    $query = sprintf("SELECT * FROM punch_clock pc WHERE YEARWEEK(pc.punch_time, 1) = YEARWEEK(%s, 1) AND pc.user_id = %d ORDER BY pc.punch_time ASC", $fromweek == "CURRENT" ? "CURDATE()" : "'$fromweek'", $userId);
} else {
    $query = sprintf("SELECT * FROM punch_clock pc WHERE YEARWEEK(pc.punch_time, 1) BETWEEN YEARWEEK(%s, 1) AND YEARWEEK(%s, 1) AND pc.user_id = %d AND pc.punch_status != '3' ORDER BY pc.punch_time ASC", "'$fromweek'", "'$toweek'", $userId);
}

$result = mysql_query($query) or die(mysql_error());

/** time logged in seconds **/
$timeLogged = 0; 
$startTime = 0;
$endTime = 0;
while($row = mysql_fetch_array($result))
{
    if ($row['punch_status'] == 1) {
        $startTime = strtotime($row['punch_time']);
    }


    if ($row['punch_status'] != 1 AND $row['punch_status'] != NULL AND $row['punch_status'] != 5 OR $row['punch_status'] == 3) {


            $endTime = strtotime($row['punch_time']);
            $timeLogged += ($endTime - $startTime);


            $endTime = null;
            $startTime = null;

    }

    if($row['punch_status'] == 5)
    {
        if($from == "")
        {
            $from = strtotime($row['punch_time']);
        } else {
            $timeLogged += (strtotime($row['punch_time']) - $from);
        }

    }


    if($row['punch_status'] == 2)
    {
        // Calculate the difference for dinner
        $timeLogged += workOutDinnerDuration(strtotime($row['punch_time']), strtotime(getLunchIn($row['punch_time'], $userId)));
    }


}

if ($startTime != null) {
    // This is to show the days live unsigned out time (No idea why i'm having to add 30 minutes that seems lost somewhere?????)
    $timeLogged += (time() - $startTime);
    $startTime = null;
}

$hours = floor($timeLogged / 3600);
$minutes = floor(($timeLogged / 60) % 60);
$seconds = $timeLogged % 60;


return sprintf('%02d:%02d', $hours, $minutes);

}

数据:

2015-06-08 08:52:03 - 1
2015-06-08 19:12:03 - 0
2015-06-09 08:38:03 - 1
2015-06-09 11:50:00 - 3
2015-06-09 13:19:23 - 1
2015-06-09 17:03:23 - 0
2015-06-10 08:42:03 - 1
2015-06-10 16:02:03 - 0

0 个答案:

没有答案