Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top