简体   繁体   中英

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. It works good when the time is 00:00:00 to 19:59:00. I could not figure as it works good for 19 hrs. 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.

 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. 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

  • $sql_time - time format in mysql

So you need to convert the Time formats from PHP to 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.

For vice versa case. MYSQL time to PHP we need to change the format you want to display. just search or play with date() function.

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