Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: ereg / preg_match time validation

  1. #11
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    <?php

    // Try this out:
    echo ( date("H:i:s"strtotime("3:30 pm")) );

    // MySQL query will select the time in 24 HOUR format and return it in 12 HOUR
    $q "SELECT TIME_FORMAT(time_column, '%r') as time FROM table WHERE ..." ;

  2. #12
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Thanks for that, but I'd already made the necessary functions...

    But while we're on the topic of validation I've made a function to validate the date as well, it works just as intended but I want to know how to return an error if the date is false.

    Like : 31/04/2009 - Passes my preg_match() function as it is valid

    But 31/04/2009 is not actually valid, and if you insert it into a database it truncates and ends up with 0000-00-00.

    I tried the following to catch the error, but having no luck - any suggestions?

    PHP Code:
    // Insert date into empty row to test it is a valid date, e.g. User could input 31/04/2009 which passes the RegExp but is not a valid date
    #MySQL error 1265 is output if data truncated
    $query "INSERT INTO appointments (`date`) VALUES('$date')";
    if(
    mysql_query($query))
    {
        if(
    mysql_errno() == 1265)
        {
            die(
    "Invalid date.");
        }


  3. #13
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

  4. #14
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    Thanks for that, but I'd already made the necessary functions..
    If you made functions for this, you shouldn't have. PHP and MySQL already have functions for this. That is what I posted above.

    Just like checkdate() .

  5. #15
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ah I see, checkdate's very helpful - but I think it's better that I've made my own functions, as I'm doing it for my A2 coursework, so will hopefully get more marks

  6. #16
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    Ah I see, checkdate's very helpful - but I think it's better that I've made my own functions, as I'm doing it for my A2 coursework, so will hopefully get more marks
    Ahh, I see. Hopefully you are right !

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •