Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with validating time
Old 06-16-2011, 04:58 PM Help with validating time
Skilled Talker

Posts: 87
Trades: 0
I'm looking for a way to get time input from a user to be inserted into a database as a MySQL TIME format, which is yyyy-mm-dd hh:mm:ss.

However, the yyyy-mm-dd hh:mm:ss is not intuitive... and is difficult to validate.

What's the most intuitive way for the user to input this time? How do I validate it is correct (in the right format)?

Thanks so much in advance.
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
 
Register now for full access!
Old 06-16-2011, 05:51 PM Re: Help with validating time
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I'm not sure if there is a 'most intuitive way' to have users input time. You could use a separate text field or dropdown for each time field (year, month, day, etc). Or you could have them input a single string which could then be parsed.

As far as validation goes, you can use checkdate for the year month and day fields. For the hours, minutes and seconds you may have to validate it manually, but that should be simple enough.

After you've done all of your validating and parsing you can use the separate time fields to construct a mysql formatted time string.

If you can settle on a particular format for inputting the time I can post a code example.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-16-2011, 05:59 PM Re: Help with validating time
Skilled Talker

Posts: 87
Trades: 0
Perhaps I'll just have them input a single string. Seems quicker for the user that way, instead of making him or her move the mouse and click on menus.

So, if I want them to input the time as yyyy-mm-dd hh:mm am/pm, i.e. 2011-06-23 2:30 pm or 2010-06-12 11:44 am and then convert this to the format I want, how would I go about doing that?

Sorry, php newbie. Thanks for all the help!
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-16-2011, 06:01 PM Re: Help with validating time
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
2nd EDIT: I think most users would not find it easier to write the whole date/time as a long string. That requires them to "think more" and to format it properly, instead of just selecting from a predefined list.

EDIT: NullPointer got to it before me, but I'll post it anyway since I already wrote it :P

Personally I think (one of) the easiest ways would be to have drop down menus for the date (year, month, day), and normal text boxes for time (hour, minute, seconds), each with a width of two characters.

Now, for the validation part.
I would run them through a regex, and use check_date() to validate the date. I'm no regex expert but I think this shoud do it.

PHP Code:
// Fetch the data and store them in $year, $month, $day, $hours, $minutes, $seconds
$time_pattern "/^(([0-1]?[0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$/";
$date_pattern "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/";

$time_str "$hours:$minutes:$seconds";
$date_str "$year-$month-$day";

if (
preg_match($time_pattern$time_str) != 1) {
   
// incorrect time format, do something
}
if (
preg_match($date_pattern$date_str) != 1) {
   
// incorect date format, do something
}
if (!
check_date($month$day$year)) {
   
// invalid date, do something

__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 06-16-2011 at 06:14 PM..
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-16-2011, 06:05 PM Re: Help with validating time
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Don't give the users a text box to input dates, present them with select elements for the input.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-16-2011, 06:05 PM Re: Help with validating time
Skilled Talker

Posts: 87
Trades: 0
@lizciz your solution gives me an idea on how to do this. Thank so much for the help, both of you!
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 06-16-2011, 07:20 PM Re: Help with validating time
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Be sure to take a look at strtotime
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with validating time
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.54832 seconds with 12 queries