February 22nd, 2013, 06:20 PM
Looking for strtotime() replacement
I have a need for a function or class that works LIKE strtotime(), but I need to modify the way it works a little.
So for starters I'm trying to find if anyone has reinvented this particular wheel.
February 22nd, 2013, 06:38 PM
February 22nd, 2013, 08:53 PM
My application has to do with animals. Recording the birth date.
Sometimes the entire date is known, sometimes just a year, or a year and a month.
Three changes I need to make.
1) output should be in Y-m-d format (which will be more self explanitary with ther next requirement)
2) Needs to be able to handle incomplete dates differently.. "February 2013" should be saved as "2013-02-00" not "2013-02-01" because the actual day is unknown. Same with months if only the year is known.
3) the current function translates "2012" as today at 10:12 PM, instead I want it recognized as a number.
optional, the time of day is irrelevant, so I can drop that completely...
Sure, I could just supply dropdown boxes for year, month and day, but I would really like to just offer a free form text box to the user.
February 22nd, 2013, 10:23 PM
I suggest you start looking for an alternative. strtotime() is complicated - it has a whole parser and lexer, treating the date string like it were a language with a grammar and syntax*.
1. See date
3. If the input is strictly a number then assume it's a year
Web users who have done anything with dates are used to seeing (a) calendars or (b) separate boxes for each field. Nothing wrong with following the norm.
Especially since strtotime() has some formats it won't recognize correctly (D/M/Y versus M/D/Y being the prime example), and that's despite how awesome it is at everything else. Without spending an inordinate amount of time on this, you won't get the same level of quality with your own version.
* This means it's not simply a matter of editing code to adapt it to your own rules. Translating it to PHP is inadvisable because the parser paradigm isn't that great when it's interpreted at runtime, not to mention being a laborious chore.