duannengling4705 2013-04-13 05:19
浏览 28
已采纳

Php时间差异返回相反的值

I have the following code:

<?php
require 'core.php';
require 'connectdb.php';

$user_id = getfield('user_id');
$_SESSION['user_id'] = $user_id;
$checker = getchecker();
     // Get current time 
    $query = "SELECT `timein`,`timeout` FROM `time` WHERE `user_id` = '$user_id'";
    if($query_run = mysql_query($query))
    {
        $query_num_rows = mysql_num_rows($query_run);
        if($query_num_rows == 0){
                    echo 'Invalid or No such registered user.';
                }else if($query_num_rows == 1){
                    $in = mysql_result($query_run,0,'timein');
                    $out = mysql_result($query_run,0,'timeout');
                    $_SESSION['in'] = $in;
                    $_SESSION['out'] = $out;
                }
    }
echo $in."  ".$out;
    $datein = strtotime($in);
    $dateout = strtotime($out);
    echo $datein."  ".$dateout;

    $dateDiff    = $datein - $dateout;
    $fullDays    = floor($dateDiff/(60*60*24));
    $fullHours   = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
    $fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
    $fullSeconds = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60)-     ($fullMinutes*60*60))/60);
    echo "Differernce is $fullDays days, $fullHours hours $fullMinutes minutes and      $fullSeconds seconds.";   
    ?>

from the database:

$in = 12:50:26
$out = 12:52:46

This is what comes out after running the code:

(time in)   (time out) (timestampin)(timestampout) (output)
12:50:26    12:52:46   1365850226   1365850366    Differernce is -1 days, 23 hours 57 minutes and -3363 seconds.

the value should be:
Difference is 0 days, 0 hours 2 minutes and 20 seconds.

I have a hunch that it has something to do with the calculation for it. What should I do to get the right answer?

  • 写回答

2条回答 默认 最新

  • douxiong3245 2013-04-13 05:53
    关注

    Please modify below lines.

    $dateDiff    = $dateout - $datein; //or better ABS($dateout - $datein)
    

    AND

    $fullSeconds = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60)-($fullMinutes*60)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算