简体   繁体   English

19:59:00之后无法在php中使用mysql插入时间

[英]Unable to insert time after 19:59:00 in mysql using php

I am not unable to insert the time value 20:00:00 or more than that. 我无法插入20:00:00或更多的时间值。 It works good when the time is 00:00:00 to 19:59:00. 时间为00:00:00到19:59:00时效果很好。 I could not figure as it works good for 19 hrs. 我无法确定它能正常工作19个小时。 I am new for developing. 我是新来的开发者。 I have made several effort to figure it out. 我已经尽力弄清楚了。 But I could'nt. 但是我不能。 Please help me. 请帮我。 I have added the html, php coding below and the mysql 'attendance' table. 我在下面添加了html,php编码和mysql'attendance'表。

 CREATE TABLE IF NOT EXISTS `attendance` (
    `id` int(11) NOT NULL,
      `shift_start_time` time NOT NULL,
      `empid` varchar(50) NOT NULL,
      `user` varchar(100) NOT NULL,
      `checkin` datetime(6) NOT NULL,
      `checkout` datetime NOT NULL,
      `worked_hours` time NOT NULL,
      `latehours` time NOT NULL,
      `late_seconds` int(11) NOT NULL,
      `Break1` time NOT NULL,
      `Break1out` time NOT NULL,
      `Break1hours` time NOT NULL,
      `Break2` time NOT NULL,
      `Break2out` time NOT NULL,
      `Break2hours` time NOT NULL,
      `Lunch_Break` time NOT NULL,
      `Lunch_Breakout` time NOT NULL,
      `Lunch_Breakhours` time NOT NULL,
      `A_Break` time NOT NULL,
      `A_Breakout` time NOT NULL,
      `A_Breakhours` time NOT NULL,
      `A_BreakReason` varchar(1000) NOT NULL,
      `totalbreak` time NOT NULL,
      `workhrs` time NOT NULL,
      `meeting_hours` time NOT NULL,
      `checkindate` date NOT NULL
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=350 ;




<form action="checkincheckout.php" method="post"><table><tr><td><label  for="line_items"                                                                    >
                                                                  <p class="text-info">Check In</p>
                                                                  </label><input type="radio" class="form-control" placeholder="Enter Project Name" name="checkin" value="<?php 
                                                                                                echo date('Y-m-d H:i:s');?> " >
    </td><td><label  for="line_items"                                                                    >
                                                                  <p class="text-info">Check Out</p>
                                                                  </label><input type="radio" class="form-control" placeholder="Enter Project Name" name="checkout" value="<?php echo date('Y-m-d H:i:s');?>"></td></tr><tr><td colspan="2"><input type="Submit" class="btn btn-primary" value="Submit" name="submit" >


    </script></td></tr></table></form>

//checkingcheckout.php

     if(isset($_POST['checkin']))
        {
            //$checkin =date("Y-m-d H:i:s", $_POST['checkin']);
                 $checkin = $_POST['checkin'];
                 $checkindate=substr('$checkin',0,10);

                 if($checkin>$_SESSION['START_TIME'])
                 {
                    $timeDiff=strtotime($checkin)-strtotime($_SESSION['START_TIME']);



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

    echo "$hours:$minutes:$seconds";
    $ela=$hours.'+'.$minutes.'+'.$seconds;

                      $sql = mysql_query("INSERT INTO `attendance`(shift_start_time,empid,user,checkin,latehours,late_seconds,checkindate) VALUES ('".$_SESSION['START_TIME']."','".$_SESSION['USERNAME']."','".$_SESSION['NAME']."','$checkin','$ela','$timeDiff','$checkindate')"); 
                 }

Before we insert to table, php time should be converted to my sql compatible time format. 在我们插入表格之前,应该将php时间转换为我的sql兼容时间格式。 You can use the below code for that 您可以使用以下代码

$date = DateTime::createFromFormat( 'H:i A', $php_time);
$sql_time = $date->format( 'H:i:s');
  • $php_time - time format in php $ php_time-PHP中的时间格式

  • $sql_time - time format in mysql $ sql_time-MySQL中的时间格式

So you need to convert the Time formats from PHP to MYSQL 所以您需要将时间格式从PHP转换为MYSQL

This is example and I have tested it 这是示例,我已经测试过

$time_from_input = date("y-m-d", (strtotime($_POST['time']));

Now this time is converted into MYSQL format. 现在,这次将转换为MYSQL格式。

For vice versa case. 反之亦然。 MYSQL time to PHP we need to change the format you want to display. 到PHP的MYSQL时刻,我们需要更改要显示的格式。 just search or play with date() function. 只需搜索或使用date()函数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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