简体   繁体   English

HTML表单输入到mysql TIME

[英]HTML form input to mysql TIME

I am having an add/edit form to update and add to database, and I was not sure what the best way is to input TIME type (HH:MM:SS). 我有一个添加/编辑表单来更新和添加到数据库,我不确定输入TIME类型(HH:MM:SS)的最佳方法是什么。 Should I use multiple html text inputs for HH, MM, SS? 我应该为HH,MM,SS使用多个html文本输入吗? if so, is there a function that prepares the string for database input? 如果是这样,是否有一个函数可以为数据库输入准备字符串?

Basically what I'm trying to input is how many hours, minutes, seconds a specific task took to finish. 基本上,我要输入的是完成一项特定任务需要多少小时,几分钟,几秒钟。

Can anyone point me in the right direction here? 有人可以在这里指出正确的方向吗?

I'm designing a website using Codeigniter (PHP). 我正在使用Codeigniter(PHP)设计一个网站。

Thanks 谢谢

Let me go ahead and clarify what needs to happen a bit more... The user is required to enter data specific to sports more in particular to a players minutes and seconds played. 让我继续,进一步说明需要发生的事情...用户需要输入特定于运动的数据,尤其是对于运动员所玩的分钟和秒。 I'm thinking of maybe simplifying it to only minutes. 我正在考虑将其简化为仅几分钟。 Perhaps this way input is only 1 thing. 也许这种方式的输入只是一件事。 Then again my question is, what method would work to convert this "minute number" to the correct MYSQL TIME format? 再一次,我的问题是,什么方法可以将这个“分钟数”转换为正确的MYSQL TIME格式?

I wrote a helper to do something similar in an app I'm working on. 我写了一个助手来在我正在开发的应用程序中做类似的事情。 Mine generates three dropdowns, hh, mm and am/pm, by calling built in form_dropdown helper. 通过调用内置的form_dropdown帮助器,Mine会生成三个下拉列表:hh,mm和am / pm。 Once I get the data from the drop downs, I convert it 24hr format and then I just concatenate the strings into the right format for MySQL. 从下拉列表中获取数据后,我将其转换为24小时格式,然后将字符串连接为适用于MySQL的正确格式。 Since it's a helper I can just call it from any view using form_time() . 因为它是一个帮助器,所以我可以使用form_time()从任何视图调用它。 I can post it here if you think it would help to see it. 如果您认为有帮助,可以在此处发布。

Dana 达娜

just use now() function, if your DB is MYSQL is you want to save the current time 只需使用now()函数,如果您的数据库是MYSQL,则要保存当前时间

eg: 例如:

 UPDATE tbl SET timemodified = NOW() 

and make sure that timemodified has a type of "time" 并确保timemodified具有“时间”类型

Ow sory miss that point. 太糟糕了,错过了这一点。 uhm maybe you need to have a start time in your DB, then after he/.she is finish his/her task. 嗯,也许您需要在数据库中有一个开始时间,然后在他/她完成任务之后。 you must query on the DB the start time subtract it to the end time(your current time) then the result would be the time he/she performed the task 您必须在数据库上查询开始时间减去结束时间(当前时间),然后得出的结果就是执行任务的时间

this checks for 2 numbers, then a ":" then 2 numbers, then a ":", then finally 2 numbers again: 这将检查2个数字,然后是“:”,然后是2个数字,然后是“:”,最后又是2个数字:

$cleanTime = preg_match( '/(\d\d)\:(\d\d)\:(\d\d)/', $_POST[ 'NAME_OF_TIME_INPUT' ] );
if( !$cleanTime ){ /* ... error ... */ }

don't be scared of all the slashes, haha (I was at first when I used regexps). 别害怕所有的斜线,哈哈(我刚开始使用正则表达式时是第一次)。

replace NAME_OF_TIME_INPUT with the content of the name attribute on the <input on the <form page NAME_OF_TIME_INPUT替换为<form页面上<input上的name属性的内容

ie if <input name="coolinput" /> then use $_POST[ 'coolinput' ] 例如,如果<input name="coolinput" />则使用$_POST[ 'coolinput' ]

This is one of the eternal struggles of (web) UI design, how to input time without driving the users nuts. 这是(Web)UI设计永恒的斗争之一,如何在不驱使用户疯狂的情况下输入时间。 What works for your specific case is something only you can decide, because it depends on the exact format/circumstances you need and your target audience. 对于您的特定情况,什么是您自己可以决定的事情,因为这取决于您需要的确切格式/情况以及您的目标受众。

As general guidelines I'd say: 作为一般准则,我会说:

  • Don't do a free-form text field that requires a certain format, eg "Enter time (HH:MM:SS)", because it's too easy to mess up and will deny the users input or mess up the time if you do no validation. 不要使用要求某种格式的自由格式文本字段,例如“输入时间(HH:MM:SS)”,因为这样做太容易搞乱了,如果您这样做,将会拒绝用户输入或弄乱时间没有验证。
  • Try to avoid [0-23] [0-59] [0-59] dropdowns, since they can be quite a pain (click, scroll, click, click, scroll, click, click, scroll, click). 尝试避免[0-23] [0-59] [0-59]下拉菜单,因为它们可能会很痛苦(单击,滚动,单击,单击,滚动,单击,单击,单击,滚动,单击)。

If ease of use is a high priority, as would be the case for public websites, maybe a Javascript enhanced timepicker is a good idea. 如果像公共网站那样优先考虑易用性,那么Javascript增强的时间选择器可能是个好主意。 Try not to use anything too fancy that nobody gets though (like dragging the hands on a clock). 尽量不要使用任何没人喜欢的花哨的东西(例如在时钟上拖动指针)。

A free-form, free-format text field might be the best idea. 自由格式,自由格式的文本字段可能是最好的主意。 The user can just type in "3pm", "16:34" or "midnight". 用户只需输入“下午3点”,“ 16:34”或“午夜”即可。 You may need to provide examples to get users started, otherwise they may feel lost. 您可能需要提供示例以使用户入门,否则他们可能会感到迷路。 You can run this through strtotime on your end, but you may need to fill in the blanks and do a lot of validation. 您可以在strtotime上运行它,但是您可能需要填写空白并进行大量验证。

Three short text fields may be a good idea if your audience is very keyboard focused and can be expected to tab through them in rapid order. 如果您的受众群体非常关注键盘,并且可以快速按顺序浏览它们,那么三个简短的文本字段可能是一个好主意。

As for formatting it for SQL, however you receive the time input from the user, you should assemble it to a UNIX timestamp and format that timestamp for SQL: 至于将其格式化为SQL,但是您会收到用户输入的时间,则应将其组装为UNIX时间戳,并格式化该时间戳以用于SQL:

date('Y-m-d H:i:s', $timestamp);

I have searched for some alternatives and solutions and I came up with this: 我搜索了一些替代方法和解决方案,然后我想到了:

$min = 60;
$time[] = floor($min/60);
$time[] = $min%60;

And I used the following to convert to MySQL TIME format 而且我使用以下内容转换为MySQL TIME格式

INSERT INTO table (min) VALUES (MAKETIME($time[0], $time[1], 0))

I have had the same issue with working with the html time input. 使用html时间输入时,我遇到了同样的问题。 However, I've managed to work around it with a PHP function. 但是,我设法使用PHP函数来解决它。

What the function does is translate the time into a format the MYSQL DATETIME datatype can understand. 该函数的作用是将时间转换为MYSQL DATETIME数据类型可以理解的格式。

Of course you will need to enter the date somehow, but I'll leave that up to you. 当然,您将需要以某种方式输入日期,但是我会留给您。

functions.php functions.php

function convertHtmlTime($date,$time){
        $newDate = date($date);
        $newTime = date($time);
        $datetime = new DateTime($newDate.$newTime);
        return date_format($datetime, 'YmdHis');
}

test.php test.php

$date="2007-02-16";
$time="23:59";
echo convertHtmlTime($date,$time);

Results: 结果:

20070216235900 20070216235900

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM