Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: ereg / preg_match time validation

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

    Default ereg / preg_match time validation

    Hi, I want to validate the time the user has entered to see whether it's in the correct format (12 hour format).

    From googling around I found:

    PHP Code:
    # Returns time in HH:MM[AM|PM] format if it matches, else false
    function checkTime($time)
    {
    return (
    preg_match('/[\s0]*(\d|1[0-2]):(\d{2})\s*([AaPp][Mm])/xms'$time$match))
    sprintf('%02d:%d%s'$match[1], $match[2], strtoupper($match[3]))
    false;

    It almost works, but it allows 21:50pm, I don't have any knowledge of ereg / preg_match so I'm finding it hard to alter it to fix it.

    Anyone know what I need to do to fix it? Or come up with a better solution?

    Thanks.
    Last edited by Schmoopy; 03-10-2009 at 12:11 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    <?php
    /*
    VALID:
    02:57am
    12:53PM

    INVAID:
    2:57am
    05:15 PM
    13:11Pm
    12:00aM
    */
    function check_12hour_time($str){
        
    preg_match('/^(0[1-9]|1[0-2]):(0[1-9]|[1-5][0-9])(AM|PM|pm|am)$/'$str$matches);
        return !
    $matches false:array('hours' => $matches[1], 'minutes' => $matches[2], 'tod' => $matches[3]);
    }
    var_dump(check_12hour_time('11:59PM'));

  3. The Following User Says Thank You to techietim For This Useful Post:

    Schmoopy (03-10-2009)

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

    Default

    That works great, thanks!
    Last edited by Schmoopy; 03-10-2009 at 09:20 AM.

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

    Default

    PHP Code:
    <?php

    $time 
    '21:50PM';

    // Returns 1 if valid, 0 if not valid, false if $time hasn't been set
    function check12Hour($time null) {

    if (!
    $time)
        return 
    false;

    return 
    preg_match('/^(0[1-9]|1[0-2]):([0-5][0-9])(AM|PM)$/'$time);

    }

    echo 
    check12Hour($time);

    ?>
    I'm no expert with regex, but I am slowly getting better.

    I think the trick is to build them one step at a time, testing it out as you go.

    For example:

    Start out with an idea of what valid is. In this case 09:00AM is valid.

    Set up the format: '//'

    Then go step by step:

    Code:
    '//'
    
    '/^09:00AM$/' // ^ means starts with, $ means ends with
    
    // The first "group" is the first two digits of the time
    // Parenthesis around a group
    // Now think about all possible values for the first two digits
    // You can have a 0 followed by a 1-9 OR a 1 followed by a 0-2
    // 0 followed by a 1-9 is written 0[1-9]
    // Use | ( the pipe char ) for OR
    
    '/^(0[1-9]|1[0-2]):00AM$/'
    
    // Next you want a single colon, so type that.
    
    // Then move on to the next group
    // Parenthesis and follow the same idea as before
    
    '/^(0[1-9]|1[0-2]):([0-5][0-9])AM$/'
    
    // Then the AM OR PM gets some parenthesis
    
    '/^(0[1-9]|1[0-2]):([0-5][0-9])(AM|PM)$/'
    
    // And that's it
    
    '/

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

    Default

    On yeah, one more thing I forgot to mention. If you are programming PHP forget you ever heard about ereg.

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

    Default

    @techietim - Your function almost worked , but didn't allow times like 05:00pm, or just any time with 2 0s for the minutes.

    @JasonDFR - Thanks for your explanation, I've tested all my extremes and different values and so far all have passed, lucky you replied too

    Thanks to both of you. Kudos.

    [edit] And yes I did hear that ereg was being replaced by preg_match, since it's much faster and something else I can't remember, I only use it because I'm not very good at matches but I should spend some more time learning patterns as it seems really important when it comes to good validation.

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

    Default

    Cool,

    I'm confident the function I posted will work for you.

    I realized techie didn't quite get it right. His problem was (0[1-9]|[1-5][0-9]) which would be ok for 01 - 09 OR 10 - 59, but not 00.

    I'm not sure why he did that. What was your thinking Techie?

  9. #8
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Quote Originally Posted by JasonDFR View Post
    I'm not sure why he did that. What was your thinking Techie?
    Whoops

    I was half asleep when I wrote that, so please excuse me.

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

    Default

    hehe, No worries. That kinda thing happens to me when I'm wide awake.

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

    Default

    Still guys, thanks for helping out - I'm getting so confused here converting 12 hour time to 24 hour for the database and then converting it back again for the user xD

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
  •