cpd Posted December 13, 2008 Share Posted December 13, 2008 Baisically i want to check the format of a date. say the format must be dd/mm/yyyy I want to check its in that format...How do i do this? Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/ Share on other sites More sharing options...
Caesar Posted December 13, 2008 Share Posted December 13, 2008 Well, your question wasn't very specific. What exactly are you checking? User input? A record stored in a database? A string? Anyway...assuming it's a string or something manually entered in a form (Bad idea to let people enter dates without forcing the format yourself).... <?php if(!preg_match('/[0-3][0-9]\/[0-1][0-9]\/2[0][0-9][0-9]/', $date) Errors('date'); //Assuming you wrote an Errors() function. else echo 'Email is formatted correctly'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714752 Share on other sites More sharing options...
cpd Posted December 13, 2008 Author Share Posted December 13, 2008 Yer thats exactly the sort of thing i wanted, would you mind explainign it coz i dont fully understand it! Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714819 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 Well, your question wasn't very specific. What exactly are you checking? User input? A record stored in a database? A string? Anyway...assuming it's a string or something manually entered in a form (Bad idea to let people enter dates without forcing the format yourself).... <?php if(!preg_match('/[0-3][0-9]\/[0-1][0-9]\/2[0][0-9][0-9]/', $date) Errors('date'); //Assuming you wrote an Errors() function. else echo 'Email is formatted correctly'; ?> Not so fast there buddy. That regex would validate 39/19/2099. Correct format but obviously a bunk date. And what if this was some kind of birthday date form? Not everybody was born in the year 2000+ Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714848 Share on other sites More sharing options...
cpd Posted December 13, 2008 Author Share Posted December 13, 2008 So how do you do it the proper way then? Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714849 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 The best way to do it is use dropdown menus for mm dd yyyy and validate each one with a condition, like check if month is between 1 and 12, etc... and just concatonate them together into your own formatted string. Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714853 Share on other sites More sharing options...
cpd Posted December 13, 2008 Author Share Posted December 13, 2008 There would be no point in a vaidation then because they would only be able to use those values. Ok then no worries i think iw ill use Drop downs in that case because i can get it exactly how i want it Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714855 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 There would be no point in a vaidation then because they would only be able to use those values. Ok then no worries i think iw ill use Drop downs in that case because i can get it exactly how i want it Always always ALWAYS validate input. Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714856 Share on other sites More sharing options...
cpd Posted December 13, 2008 Author Share Posted December 13, 2008 MEGA LOL Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714858 Share on other sites More sharing options...
DarkWater Posted December 13, 2008 Share Posted December 13, 2008 MEGA LOL I hope you're not "MEGA LOL"-ing at his word of warning, because you can easily edit dropdown menus and send WHATEVER you want to a script if you have half a brain. Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714859 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 No, seriously. Consider this simple form: <form action='somepage.php' method = 'post'> <select name = 'blah'> <option value = '1'>1</option> <option value = '2'>2</option> <option value = '3'>3</option> </select> <input type = 'submit' value = 'submit'> </form> If you do not check to see if $_POST['blah'] is 1,2 or 3. I can do: Rightclick view source highlight and copy form paste into my own html file change one of the values to anything I want change the action= from somepage.php to http://www.yoursite.com/somepage.php save load up my file in my browser choose the selection with my altered data submit Your form takes it without validating, and you get wtfpwned. Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714862 Share on other sites More sharing options...
cpd Posted December 13, 2008 Author Share Posted December 13, 2008 :S:S Oh crap, i didnt realise you could do that. Ouch. Well thats no problemo because i can just check the format of it Or make a quick function to check the format etc! Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-714863 Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 So how do i check if its from say 1-12 or the the Month is not from Jan to Dec Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715070 Share on other sites More sharing options...
.josh Posted December 14, 2008 Share Posted December 14, 2008 you would use a condition, like an if statement or something. That's pretty basic stuff. There are lots of tutorials out there for php basics. Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715162 Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 yer but i dont wana write an if statement say if($VAUE != 1 || $VALUE != 2 etc etc That would take ages Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715168 Share on other sites More sharing options...
sasa Posted December 14, 2008 Share Posted December 14, 2008 try <?php $test = '29/02/2001'; $test = explode('/', $test); $comp = explode('/',date('d/m/Y',mktime(1,1,1,$test[1],$test[0],$test[2]))); if($test[0]==$comp[0] and $test[1]==$comp[1] and $test[2]==$comp[2]) echo 'OK'; else echo 'no'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715187 Share on other sites More sharing options...
cpd Posted December 14, 2008 Author Share Posted December 14, 2008 Could you explain what mktime does and how it works because i dont understand the php.net explanation Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715188 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2008 Share Posted December 14, 2008 To test if a date is valid - http://us3.php.net/checkdate yer but i dont wana write an if statement say if($VAUE != 1 || $VALUE != 2 etc etc If you need to test that a value is or is not in a range of values, there are more ways to do a comparison besides == and != http://us.php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715191 Share on other sites More sharing options...
.josh Posted December 14, 2008 Share Posted December 14, 2008 // example number $x = 10; // example 1 if (($x >= 1) && ($x <= 100)) { echo "$x is between 1 and 100...and I didn't have to write 100 comparisons!"; } // example 2 if (in_array($x, range(1,100))) { echo "$x is between 1 and 100...and I didn't have to write 100 comparisons!"; } you would use a condition, like an if statement or something. That's pretty basic stuff. There are lots of tutorials out there for php basics. Quote Link to comment https://forums.phpfreaks.com/topic/136849-solved-checking-the-format-of-a-date/#findComment-715314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.