因此,我尝试使用输入文本字段将时间插入到数据类型为TIME的数据库表中。
我想要插入的时间格式应该是这样的:
H:MM pm//示例: 6:30 pm
问题是am/pm没有插入到我的数据库表中。只有小时和分钟。
请告诉我如何解决这个问题。最好使用示例代码。谢谢。
发布于 2012-12-05 00:27:31
数据类型TIME用于存储时间数据类型-这意味着没有AM/PM。将数据以24小时格式存储在您的数据库中,并使用以下方法之一将其格式化为12小时格式(在PHP或MySQL中使用am/pm ):
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');或者:
$date = date('h:i:s a', strtotime($mysql_column['time']));或MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;发布于 2012-12-05 00:25:12
将日期存储为标准格式(18:30:00),以及在显示时所需的格式(使用DateTime对象或TIME函数)。
MySQL在存储时间数据时不支持额外的格式。
发布于 2014-12-17 16:56:03
我认为您希望将jquery时间选择器值添加到数据库中,并在数据库中使用实际格式。
这里我写了一些函数
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db您只需调用该函数并将返回值插入到数据库中。在打印时,您可以执行类似于echo date("h:i A",strtotime($time));的操作
https://stackoverflow.com/questions/13707206
复制相似问题