我想知道,如果我通过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.20now 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.20thanks 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]; // 140And 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 );更多推荐
发布评论