I m entering date in front end as 10:00 AM , 12:00 PM etc...( means 12 Hours format). now I want to save that value in database in time datatype column. How do I save that AM PM value into time datatype in MySQL and again want to display time appending AM PM on front end?
3条回答 默认 最新
- douhuang4166 2010-01-18 14:03关注
To insert:
# replace first argument of STR_TO_DATE with value from PHP/frontend TIME( STR_TO_DATE( '10:00 PM', '%h:%i %p' ) );
To select:
# replace first argument with your time field TIME_FORMAT( '22:00:00', '%h:%i %p' );
EDIT:
I'll just go ahead and presume you use mysql lib functions.// 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 );
Then to retrieve the values:
$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 );
This will return a result set where the formatted data is in the
*_formatted
fieldsEDIT:
Adjusted%m
(month) to%i
(minutes). A thank you to Donny for the well spotted slip up.本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥50 求解vmware的网络模式问题
- ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
- ¥15 springboot 3.0 实现Security 6.x版本集成
- ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
- ¥30 请帮我解决一下下面六个代码
- ¥15 关于资源监视工具的e-care有知道的嘛
- ¥35 MIMO天线稀疏阵列排布问题
- ¥60 用visual studio编写程序,利用间接平差求解水准网
- ¥15 Llama如何调用shell或者Python
- ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?