表格值00:00:00成时间值(form value 00:00:00 into time value)

编程入门 行业动态 更新时间:2024-10-28 02:34:00
表格值00:00:00成时间值(form value 00:00:00 into time value)

我想知道,如果我通过javascript函数记录时间并说我让它保持2分20秒:

00.02.20

现在,当我将其插入到我的数据库中时,字段类型设置为时间,它不能正确记录。

我认为它来自我如何请求数据:

$length = mysql_escape_string($_REQUEST['timeDrive']);

有没有办法将其转换为输出确切值的时间:

00.02.20

谢谢您的帮助!

I was wondering, if I record a time through a javascript function and say I leave it on for 2 mins 20 seconds:

00.02.20

now when I insert this into my database, where the field type is set as time, it doesn't record properly.

I think it comes from how I request the data:

$length = mysql_escape_string($_REQUEST['timeDrive']);

is there a way of converting this into a time that is going to output the exact value:

00.02.20

thanks for the help!

最满意答案

如果您不需要在这些字段上执行任何数学函数(例如:求和,平均等),并且它纯粹用于显示,那么您可以将它作为纯文本存储在CHAR(8)字段中。

否则,您需要将其标准化为某种单位。 我建议可能是总秒数的整数(如果这对你来说足够准确)。

要转换为秒:

$val = "00.02.20"; $parts = explode(".", $val); // ['00', '02', '20'] $totalSeconds = parts[0] * 3600 + parts[1] * 60 + parts[2]; // 140

并将其转换回来:

$seconds = 140; // this would be read from the database, or something printf("%02d.%02d.%02d", $seconds / 3600, ($seconds / 60) % 60, $seconds % 60 );

If you don't need to perform any mathematical functions on those fields (eg: sum, average, etc), and it's purely for display, then you can just store it as plain text in a CHAR(8) field.

Otherwise, you'll need to normalise it to some sort of unit. I'd suggest maybe as an integer of the total number of seconds (if that is accurate enough for you).

To convert to seconds:

$val = "00.02.20"; $parts = explode(".", $val); // ['00', '02', '20'] $totalSeconds = parts[0] * 3600 + parts[1] * 60 + parts[2]; // 140

And to convert it back:

$seconds = 140; // this would be read from the database, or something printf("%02d.%02d.%02d", $seconds / 3600, ($seconds / 60) % 60, $seconds % 60 );

更多推荐

本文发布于:2023-07-21 09:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1209028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表格   时间   time   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!