Php 将AM PM时间字符串存储到MySQL中的时间数据类型中,并在显示时使用AM PM进行检索?
我在前端输入的日期为上午10:00、下午12:00等(表示12小时格式)。Php 将AM PM时间字符串存储到MySQL中的时间数据类型中,并在显示时使用AM PM进行检索?,php,mysql,time,append,Php,Mysql,Time,Append,我在前端输入的日期为上午10:00、下午12:00等(表示12小时格式)。 现在,我想将该值保存在数据库中的timedatatype列中。如何将AM PM值保存到MySQL中的时间数据类型中,并再次希望在前端显示时间追加AM PM?使用MySQL函数日期格式和%p参数插入: # replace first argument of STR_TO_DATE with value from PHP/frontend TIME( STR_TO_DATE( '10:00 PM', '%h:%i %p' )
现在,我想将该值保存在数据库中的timedatatype列中。如何将AM PM值保存到MySQL中的时间数据类型中,并再次希望在前端显示时间追加AM PM?使用MySQL函数日期格式和%p参数插入:
# 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' );
编辑:我将继续假设您使用mysql库函数
// 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 );
这将返回一个结果集,其中格式化数据位于*\u formatted
字段中
编辑:将
%m
(月)调整为%i
(分钟)。A谢谢唐尼,因为他犯了个大错。这就是解决办法
在您的视图中------$('#schedule_time'+).datetimepicker({pickDate:false,格式:'h:ma',minTime:new Date(),useCurrent:false,autoClose:false})
当插入datbase--“计划时间”=>date('H:i:s',strottime($this->input->post('schedule\u time'))时
当show------第一个:您不应该在查询中直接使用$\u POST。。。真正地您应该首先清理这些值。但是为了更好地帮助您:您是否也可以显示执行此查询的PHP代码?(如果愿意,请更新您的问题,而不是发表评论。因为这样更容易阅读。谢谢)。
TIME(stru-to-u-DATE(“.$open.”,“%h:%m%p”)
给出解析错误应该是TIME(STR-to-u-DATE(“$open.”,“%h:%m%p”))
当我运行上述代码时,它会显示错误日期\u关闭\u时间不能为空
更正,应为:时间\u格式('22:00:00','%h:%i%p')代码>请参见此处: