473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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","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
8 1291
"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
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
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
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
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
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
:D excellent. then I just need the first letter after each integer, and
the rest should be easy.
thank you.

May 1 '06 #8
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rob Meade | last post by:
Hi all, Ok - this leads on from speaking to a couple here and in the SQL server group... I've an application which allows the user to type in their text into a form, they add 'happy' tags...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
8
by: Dimitri Furman | last post by:
Given: Access 2002/2003 A subform in datasheet or continuous view, placed on a tab page (this last may or may not matter) Conditional formatting applied to some controls on the subform - format...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
4
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
10
by: Coleen | last post by:
Hi all :-) I have a weird formatting problem with an HTML table that I am populating using VB.Net and HTML. Here is the snippet of code for the cell I'm trying to format: Dim...
25
by: mdh | last post by:
Hi Group, Not looking for an answer, but more of an explanation. Thinking back to those heady days when you had the time to do them, may I ask this. Exercise 1-22 asks for a program to "fold"...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
4
by: Ken Wigle | last post by:
All, I would be very grateful for any help on this question. I have an application in asp.net 2.0 where I dynamically create a datatable and then bind that to a gridview. Unfortunately, the...
7
by: christery | last post by:
Anyone got a clue to why ther is a T between date and time in the "formatted for sorting" or whatewer they call it, and a Z at the end after seconds- got it fixed for my cobol programmer by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.