PHP DateTime:天数显示不正确

时间:2017-08-24 16:00:24

标签: php datetime

我正在尝试使用PHP和Jquery创建时间管理系统。我已经能够将时钟输入,开始午餐,结束午餐,时钟输出数据记录到数据库中,并按用户检索该信息。我添加了clock-in / start-lunch和end-lunch / clock-out之间的区别,以便我可以将该值插入数据库。
所以这些是价值观:

Clock-in: 2017-08-20 17:47:43  Start-Lunch: 0000-00-00 00:00:00
End-Lunch: 0000-00-00 00:00:00 Clock-out: 2017-08-24 11:50:09

结果间隔为015:05:37:52

秒,分和小时显示正确,但日子看起来很疯狂。天应该是4,但它说的是15?

下面是PHP代码:

$update = "UPDATE timeclock SET CLOCK_OUT = NOW() WHERE CLOCK_IN = ( SELECT * FROM (SELECT MAX(CLOCK_IN) FROM timeclock WHERE USERNAME = '$sessuser' order by USERNAME DESC Limit 1) t);";
$query = mysqli_query($dbc, $update);

$select = "SELECT CLOCK_IN, START_LUNCH, END_LUNCH, CLOCK_OUT FROM timeclock WHERE USERNAME = '$sessuser' ORDER BY CLOCK_IN DESC LIMIT 1;";
    $query1 = mysqli_query($dbc, $select) or die(mysqli_error($dbc));
    $row = mysqli_fetch_assoc($query1); 

    $clockin = $row["CLOCK_IN"];
    $startlunch = $row['START_LUNCH'];
    $endlunch = $row['END_LUNCH'];
    $clockout =  $row['CLOCK_OUT'];

    if($clockin !="0000-00-00 00:00:00" && $startlunch == "0000-00-00 00:00:00" && $endlunch == "0000-00-00 00:00:00" && $clockout != "0000-00-00 00:00:00") {
        echo $clockin . " " . $startlunch . "<br />";
        echo $endlunch . " " . $clockout . "<br />";

        $dateclockin = new DateTime($clockin);
        $datestartlunch = new DateTime($startlunch);
        $dateendlunch = new DateTime($endlunch);
        $dateclockout = new DateTime($clockout);

        $intval1 = date_diff($datestartlunch, $dateclockin);
        $intval2 = date_diff($dateendlunch, $dateclockout);

        $period1 = $intval1->format("%Y:%m:%d:%H:%I:%S");
        $period2 = $intval2->format("%Y:%m:%d:%H:%I:%S");

        echo $period1;
        echo $period2;

        function time_to_interval($time) {
            $parts = explode(':',$time);
            $parts[2] -= 1;

            //echo $parts[2];

            return new DateInterval('P' . $parts[2].'DT'.$parts[3].'H'.$parts[4].'M'.$parts[5].'S');
        }

        function add_intervals($a,$b) {
            $zerodate = new DateTime('0000-00-00 00:00:00');
            $dt = clone $zerodate;
            $dt->add($a);
            $dt->add($b);
            return $zerodate->diff($dt);
        }

        function format_interval_dhhmmss($interval){
            $totalhours =  0; //($interval->a);
            return $totalhours.$interval->format('%d:%H:%I:%S');
        }

        $interval1 = time_to_interval($period1);
        $interval2 = time_to_interval($period2);
        $interval3 = add_intervals($interval1,$interval2);

        $totalhrs = format_interval_dhhmmss($interval3);

        echo "This is total hours" . $totalhrs;

    }

0 个答案:

没有答案