简体   繁体   中英

MySQL time value in query 838:59:59?

i would like to ask .

I'm trying to add TIME value into mysql database .But the value added in database is 838:59:59

And it only store into timein column as 838:59:59 . Timeout column result is 00:00:00 value.

The code as below

<?php
    $connect      = mysqli_connect("localhost", "root", "root", "database");
    global $connect;   

    if(isset($_POST['Submit']))
    {   
        $timeout            = strtotime('08:30:00');
        $timein             = strtotime('12:30:00');

        $time_out_user      = strtotime($_POST['timeout']);
        $time_in_user       = strtotime($_POST['timein']);

        if(($time_out_user >= $timeout) && ($time_out_user <= $timein))
        {
            echo "Duplicate time";
        }
        else
        {
            $add         = "INSERT INTO table (timeout,timein)
                                       VALUES ('$time_out_user','$time_in_user')";
            $addDateTime = mysqli_query($connect,$add);
            echo "Time added";
        }
    }
?>
<form action="test.php" method="post">  
    <table> 
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time Out: </td>
            <td><input type ="time" name="timeout" size="30"></td>
        </tr>
        <tr>
            <td><i class="fa fa-unlock-alt"></i> </td>
            <td>Time In: </td>
            <td><input type ="time" name="timein" size="30"></td>
        </tr>
    </table>    

    <p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>              
</form>

Thanks.

The answer is simply, it's the maximum that field can hold.

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

You would probably be better off just using an int field (where the value is stored as seconds difference from the starting time). It's a common practice to have a field that stores seconds elapsed since epoch rather than a datetime field anyway. Else you would need to switch to a datetime field.

http://dev.mysql.com/doc/refman/5.7/en/time.html

You are using strtotime()

strtotime() - Parse English textual datetimes into Unix timestamps:

Eg:

 echo(strtotime("3 October 2005")); 
output  as 1128312000

HERE TRY THIS

$epoch_time_out_user  =strtotime($_POST['timeout'])- (300*60);

$dt = new DateTime("@$epoch_time_out_user"); 
$time_out_user = $dt->format('H:i:s');

//now $time_out_user has time 
// now you can use **$time_out_user**  in insert query
// do same for  **$time_in_user**  

//FYI : The value (300*60) is depends timezone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM