![](/img/trans.png)
[英]insert null date / time into mysql with php causing 0000-00-00 / 00:00:00
[英]Unable to insert time after 19:59:00 in mysql using php
我无法插入20:00:00或更多的时间值。 时间为00:00:00到19:59:00时效果很好。 我无法确定它能正常工作19个小时。 我是新来的开发者。 我已经尽力弄清楚了。 但是我不能。 请帮我。 我在下面添加了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')");
}
在我们插入表格之前,应该将php时间转换为我的sql兼容时间格式。 您可以使用以下代码
$date = DateTime::createFromFormat( 'H:i A', $php_time);
$sql_time = $date->format( 'H:i:s');
$ php_time-PHP中的时间格式
$ sql_time-MySQL中的时间格式
所以您需要将时间格式从PHP
转换为MYSQL
这是示例,我已经测试过
$time_from_input = date("y-m-d", (strtotime($_POST['time']));
现在,这次将转换为MYSQL格式。
反之亦然。 到PHP的MYSQL时刻,我们需要更改要显示的格式。 只需搜索或使用date()
函数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.