formatting time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    formatting time

    hi,
    how might I convert a string like "5p" or "5pm" or "5:00p" or "5.00pm"
    etc..
    to "17:00:00" (24-hour time)
    I want it so that it's really *NOT* picky and will accept just about
    anything as long as it resembles a time.

    Also, I have two times, a start time ($start) and an endtime ($end).
    For the end time, users should be able to enter values like "3h" or "3
    hours" or "3 hrs", "3h5min","3hrs5 minutes6s" etc etc... which would
    then be added to the start time, and converted to 24-hour time again.
    basically anything that starts with "a" should be interpreted as am,
    p->pm, h->hours, m->minutes,s->seconds. the trouble I'm having is
    parsing the string, so it seperates numbers and letters, and interprets
    them properly.

    also, a single number like "5" would be interpreted as "17:00:00". I
    figure any number greater than 6 and less than 12 would be assumed AM,
    and "12" would be noon, and any number greater than or equal to 1 and
    less than 6 would be assumed PM if not specified. numbers greater than
    or equal to 24 could be assumed to be hours...

    any help would be appreciated.

  • Dana Cartwright

    #2
    Re: formatting time

    "Mark" <mnbayazit@gmai l.com> wrote in message
    news:1146206995 .379248.8090@i4 0g2000cwc.googl egroups.com...[color=blue]
    > hi,
    > how might I convert a string like "5p" or "5pm" or "5:00p" or "5.00pm"
    > etc..
    > to "17:00:00" (24-hour time)
    > I want it so that it's really *NOT* picky and will accept just about
    > anything as long as it resembles a time.
    >
    > Also, I have two times, a start time ($start) and an endtime ($end).
    > For the end time, users should be able to enter values like "3h" or "3
    > hours" or "3 hrs", "3h5min","3hrs5 minutes6s" etc etc... which would
    > then be added to the start time, and converted to 24-hour time again.
    > basically anything that starts with "a" should be interpreted as am,
    > p->pm, h->hours, m->minutes,s->seconds. the trouble I'm having is
    > parsing the string, so it seperates numbers and letters, and interprets
    > them properly.
    >
    > also, a single number like "5" would be interpreted as "17:00:00". I
    > figure any number greater than 6 and less than 12 would be assumed AM,
    > and "12" would be noon, and any number greater than or equal to 1 and
    > less than 6 would be assumed PM if not specified. numbers greater than
    > or equal to 24 could be assumed to be hours...
    >
    > any help would be appreciated.[/color]

    You haven't said anything about reading the PHP manual. So I'll assume you
    haven't (which is a no-no). Go read the manual, looking at strtotime() and
    date(), and then if you don't see how to do what you want, come back here
    and ask more specific questions.

    -Dana


    Comment

    • Rik

      #3
      Re: formatting time

      Dana Cartwright wrote:[color=blue]
      > You haven't said anything about reading the PHP manual. So I'll
      > assume you haven't (which is a no-no). Go read the manual, looking
      > at strtotime() and date(), and then if you don't see how to do what
      > you want, come back here and ask more specific questions.[/color]

      strtotime() is highly unreliable in my experience, it just tries to make the
      best of it.
      A whopper of a regex and preg_replace could try to take care of it, but it
      is a lot of work to catch all possible formats as described.

      Personally, I wouldn't consider even writing this kind of code, unless
      forced by an already existing badly configured database.
      [color=blue]
      > For the end time, users should be able to enter values like "3h" or "3
      > hours" or "3 hrs", "3h5min","3hrs5 minutes6s" etc etc...[/color]

      If I'd write the required script/application/whatever I'd make it impossible
      to enter values like this. In a webpage I would create 2 simple select
      lists: one with hours and one with minutes, takes care if the whole deal.
      Controlling user-input is more reliable and a hell of a lot less work
      compared to guessing what users are trying to do.

      Grtz,
      --
      Rik Wasmus


      Comment

      • Mark

        #4
        Re: formatting time

        well, the whole idea is to make as easy as possible for the user. i
        find having two drop downs is just annoying. anyways, I'll try using
        strtotime. it doesn't have to be perfect.. hopefully it'll catch most
        cases.

        Comment

        • Geoff Berrow

          #5
          Re: formatting time

          Message-ID: <1146265149.790 751.138630@v46g 2000cwv.googleg roups.com> from
          Mark contained the following:
          [color=blue]
          >well, the whole idea is to make as easy as possible for the user. i
          >find having two drop downs is just annoying. anyways, I'll try using
          >strtotime. it doesn't have to be perfect.. hopefully it'll catch most
          >cases.[/color]

          You could just use two text boxes, one for hours, one for minutes and
          then check that the contents were integers using intval()

          --
          Geoff Berrow (put thecat out to email)
          It's only Usenet, no one dies.
          My opinions, not the committee's, mine.
          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

          Comment

          • Mark

            #6
            Re: formatting time

            eh..well. thanks, but no. i love the idea of it just being "smart".
            google knows what i'm talking about.
            strtotime works alright, doesnt recognize everything i want it to, but
            oh well. it has a nice second parameter, so i can make it relative to
            the start time :)

            it really shouldnt be too hard to break it up into integer and letter
            components though.. maybe i'll have fun with regex or whatever later :p

            Comment

            • Rik

              #7
              Re: formatting time

              Mark wrote:[color=blue]
              > eh..well. thanks, but no. i love the idea of it just being "smart".
              > google knows what i'm talking about.
              > strtotime works alright, doesnt recognize everything i want it to, but
              > oh well. it has a nice second parameter, so i can make it relative to
              > the start time :)
              >
              > it really shouldnt be too hard to break it up into integer and letter
              > components though.. maybe i'll have fun with regex or whatever later[/color]

              To just "catch" the integers, you could
              preg_match("/[0-9]+/",$input,$match es);

              Grtz,
              --
              Rik Wasmus


              Comment

              • Mark

                #8
                Re: formatting time

                :D excellent. then I just need the first letter after each integer, and
                the rest should be easy.
                thank you.

                Comment

                • Rik

                  #9
                  Re: formatting time

                  Mark wrote:[color=blue]
                  > D excellent. then I just need the first letter after each integer,
                  > and
                  > the rest should be easy.
                  > thank you.[/color]

                  preg_match_all( "/([0-9]+)[\s]*([a-zA-Z]?)/i",$input,$matc hes);

                  Regex workbench explains it like this:

                  Capture
                  Any character in "0-9"
                  + (one or more times)
                  End Capture
                  Any character in "\s"
                  * (zero or more times)
                  Capture
                  Any character in "a-zA-Z"
                  ? (zero or one time)
                  End Capture

                  Matching: 3h
                  0 => 3h
                  1 => 3
                  2 => h
                  Matching: 3 hours
                  0 => 3 h
                  1 => 3
                  2 => h
                  Matching: 3 hrs
                  0 => 3 h
                  1 => 3
                  2 => h
                  Matching: 3h5min
                  0 => 3h
                  1 => 3
                  2 => h
                  0 => 5m
                  1 => 5
                  2 => m
                  Matching: 3hrs5 minutes6s
                  0 => 3h
                  1 => 3
                  2 => h
                  0 => 5 m
                  1 => 5
                  2 => m
                  0 => 6s
                  1 => 6
                  2 => s

                  But still... typos can be made, what if someone types "5hours 6ninutes"? How
                  are you going to interpret "n", which is next to both "h" and "m"?
                  I still think it shouldn't be done this way, but hey, it's your call

                  Grtz,
                  --
                  Rik Wasmus


                  Comment

                  Working...