By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,197 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,197 IT Pros & Developers. It's quick & easy.

formatting time

P: n/a
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","3hrs5minutes6s" 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.

Apr 28 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Mark" <mn*******@gmail.com> wrote in message
news:11********************@i40g2000cwc.googlegrou ps.com...
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","3hrs5minutes6s" 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.


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
Apr 28 '06 #2

P: n/a
Rik
Dana Cartwright wrote:
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.
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.
For the end time, users should be able to enter values like "3h" or "3
hours" or "3 hrs", "3h5min","3hrs5minutes6s" etc etc...


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
Apr 28 '06 #3

P: n/a
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.

Apr 28 '06 #4

P: n/a
Message-ID: <11**********************@v46g2000cwv.googlegroups .com> from
Mark contained the following:
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.


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/
Apr 29 '06 #5

P: n/a
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

May 1 '06 #6

P: n/a
Rik
Mark wrote:
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


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

Grtz,
--
Rik Wasmus
May 1 '06 #7

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

May 1 '06 #8

P: n/a
Rik
Mark wrote:
D excellent. then I just need the first letter after each integer,
and
the rest should be easy.
thank you.


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

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
May 2 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.