我在前端输入日期为上午10:00,中午12:00等…(表示12小时格式).
现在我想在time数据类型列中保存数据库中的值.如何在AM中将AM PM值保存为时间数据类型,并再次显示在前端附加AM PM的时间?
现在我想在time数据类型列中保存数据库中的值.如何在AM中将AM PM值保存为时间数据类型,并再次显示在前端附加AM PM的时间?
要插入:
原文链接:https://www.f2er.com/php/134574.html# replace first argument of STR_TO_DATE with value from PHP/frontend TIME( STR_TO_DATE( '10:00 PM','%h:%i %p' ) );
选择:
# replace first argument with your time field TIME_FORMAT( '22:00:00','%h:%i %p' );
// first sanitize the $_POST input // also,make sure you use quotes to identify the $_POST keys $open = MysqL_real_escape_string( $_POST[ 'MondayOpen' ] ); $close = MysqL_real_escape_string( $_POST[ 'MondayClose' ] ); // this is the query,which should work just fine. $sql = ' INSERT INTO `table_lib_hours` SET `day_name` = "Monday",`day_open_time` = TIME( STR_TO_DATE( "' . $open . '","%h:%i %p" ) ),`day_close_time` = TIME( STR_TO_DATE( "' . $close . '","%h:%i %p" ) ) '; $result = MysqL_query( $sql );
然后检索值:
$sql = ' SELECT `day_open_time`,`day_close_time`,TIME_FORMAT( `day_open_time`,"%h:%i %p" ) as day_open_time_formatted,TIME_FORMAT( `day_close_time`,"%h:%i %p" ) as day_close_time_formatted FROM `table_lib_hours` WHERE `day_name` = "Monday" '; $resultset = MysqL_query( $sql );
这将返回结果集,其中格式化数据位于* _formatted字段中
编辑:调整后的%m(月)到%i(分钟).感谢Donny发现了一个很好看的漏洞.