views:

203

answers:

3

I'm looking for a way to turn user input into normalized data fit for calculation. The input is some length of time, and is coming from Twitter, so it's string only. Ideally I'd love these results:

an hour and a half --> 01:30

27.52 --> 00:28

5:24 --> 05:24

Is this is a particularly difficult thing to do? I could focus on coaching the users on how to create good input, but also don't want to be too strict.

Any suggestions would be great,

Thanks!

+2  A: 

I think the best you'll get is the strtotime (http://us3.php.net/strtotime) function.

Matt
+7  A: 

Three things to make it work:

  1. regular expressions
  2. fuzzy logic (lots of if/else statements)
  3. strtotime to normalize to standard unix epoch time (which makes additions/subtractions very easy)

Since it's user input and non-deterministic that's the only way. Even then the results won't be 100% reliable and a small margin of error should be acceptable.

aleemb
+1  A: 

+1 unix timestamps

Flavius