Help with validating time

Discussion in 'PHP' started by tanyania, Jun 26, 2011.

  1. #1
    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.
     
    tanyania, Jun 26, 2011 IP
  2. ntomsheck

    ntomsheck Peon

    Messages:
    87
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I guess my first question would be, is there a rock solid reason the user needs to enter the time? The date, I can understand, but does your application have that high of granularity in other aspects? Would it be easier for PHP to add it's own time when the user interacts? To format it, you'll have to specify operators for the user, like "please enter in MM/DD/YYYY HH:SS" format, then check for this format using preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/",$date) and preg_match("/[0-9]{2}\:[0-9]{2}/",$time); or a combination thereof. Then, convert it to epoch time for best handling. There is a MySQL function that will convert epoch time into the yyyy-mm-dd hh:mm:ss format in one fell swoop.
     
    ntomsheck, Jun 27, 2011 IP
  3. codebreaker

    codebreaker Well-Known Member

    Messages:
    281
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #3
    My guess is to use separate boxes like :
    Year :
    Month:
    Day:
    Hour:
    Minute:
    and validate them separately. I think that should work just fine
     
    codebreaker, Jun 27, 2011 IP