TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-28-2009, 01:59 PM   #1 (permalink)
The Contributor
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 98
Thanks: 8
captainmerton is on a distinguished road
Default 24hr time validation

Anyone got a simple function that will validate a time input into form (i.e. should be in format 00:00:00)? If not, what's the best way to do this? Cheers.
captainmerton is offline  
Reply With Quote
Old 06-29-2009, 01:38 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

Validating time from a string is hard, there are so many formats it is difficult for you to determine. I suggest making drop down boxes for each field, this gives them no leeway in how to format things. When it comes to input you have the format, the less leeway you give your users the better. You wouldn't think it, but someone will always come around with some weird way they format it that breaks it. Humans can validate information infinitely better than computers, your users likely do not know this.
Village Idiot is offline  
Reply With Quote
Old 06-29-2009, 04:50 PM   #3 (permalink)
The Contributor
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 98
Thanks: 8
captainmerton is on a distinguished road
Default

i suppose what i'm getting at is if i wanted the date entered in format 00:00:00 what is the best way to validate that? ereg, preg, explode etc?
captainmerton is offline  
Reply With Quote
Old 06-29-2009, 04:56 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

The simplest way would be to do this (short answer: use explode):
PHP Code:
$time="10:15:43";
$tPieces explode(":",$time);
$hour $tPieces[0];
$min $tPieces[1];
$sec =$tPieces[2]; 
Village Idiot is offline  
Reply With Quote
Old 06-29-2009, 05:31 PM   #5 (permalink)
The Contributor
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 98
Thanks: 8
captainmerton is on a distinguished road
Default

thanks. That was my initial inclination. Think i'll go with that. Wondered if using ereg or preg would be better but dont really know so much about those functions.
captainmerton is offline  
Reply With Quote
Old 06-29-2009, 05:36 PM   #6 (permalink)
The Contributor
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 98
Thanks: 8
captainmerton is on a distinguished road
Default

i.e. how would i validate that the character ':' isnt in the correct places? What would be in the array if $time was 00000000?
captainmerton is offline  
Reply With Quote
Old 06-29-2009, 05:41 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Quote:
Originally Posted by captainmerton View Post
What would be in the array if $time was 00000000?
Try it and find out. A regular expression for something like this would be a much better step towards validation (the explode snippet doesn't validate anything!) and it wouldn't need to be particularly complicated. Have a stab at it.
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 06-29-2009, 05:42 PM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

Regular expressions would be the best way to do that, use this expression:
[0-9]{2}:[0-9]{2}:[0-9]{2}
Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
php time? DizzyD General 20 06-10-2009 10:11 PM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 1 11-11-2007 07:20 PM
Two Different Ways to Format the Time CMellor Tips & Tricks 0 09-24-2007 11:12 PM
Script Execution Time Class Wildhoney Script Giveaway 2 09-14-2007 11:36 PM
Calculating the Execution Time Wildhoney Tips & Tricks 0 09-14-2007 11:14 PM


All times are GMT. The time now is 06:36 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design