Page 1 of 1
Any script out there to reliably convert to military time?
Posted: Sat Sep 05, 2009 8:21 pm
by Rumrunner
I need something that is smart enough to figure it out even if the user forgets to put in am or pm.
I figured this must be common so I was wondering if there was an efficient piece of code already written.
Thanks
Re: Any script out there to reliably convert to military time?
Posted: Sat Sep 05, 2009 9:40 pm
by requinix
Besides assuming AM unless the hour is >12?
Try
strtotime.
Re: Any script out there to reliably convert to military time?
Posted: Sat Sep 05, 2009 10:24 pm
by Rumrunner
Thanks,
I think the strtotime would work only assuming the user put in a known format.
It woudn't be so bad if I could just look for the presence of and an "a" or "p" in the string, but I am afraid the user might put 9:00 and mean pm but not put pm.
I could just put a note saying military format, but I don't want life to be difficult for them

.
So then to prevent a messup, I think I would have to make a function to check if the first two digits are greater than 12 or see if there is a 0 in the first position and also check for an "a" or "p" in the string just to minimize problems.
Dang I guess i'll have to stop being lazy and write it out...
Re: Any script out there to reliably convert to military time?
Posted: Sun Sep 06, 2009 12:12 am
by requinix
You can't just let the user do whatever they want. There have to restrictions of some kind, otherwise you're just getting (as far as you can tell) random input.
strtotime is good at guessing what was typed. If it looks like some kind of date and/or time format then strtotime will probably figure it out.