PHP”s “strtotime” function is one of the coolest core functions of the language. I never really noticed how great it was until today.
At my company, we import date formats from customers and need to store them in MySQL format. (“Y-m-d h:m:s”). In PHP, you can pass strtotime just about anything and – with the help of the date function – convert that wiley timestamp to what you want.
Consider the following time formats:
Let”s convert these to MySQL timestamps with PHP:
This automagically outputs:
I remember the first time I can across strtotime and thought “wow - it will take in just about any format.” Clearly, there are some limits (I can”t enter “orange” and get a date back) – but needless to say, the function is simple and powerful.
Back to today at work.
A developer (we’ve recently moved to being an all-Java shop) tells me he has to write a custom date parser for ingesting and converting partner’s date formats. And every time we see a new date format, he has to add to his parser (then we have to build it, QA it, release it).
Poppy cock, I say! Isn”t there something in Java like strtotime? Turns out? No.
I did my googling, nothing. Then I turned to Twitter. Watch as we go from “But you can do that in Java!” to “oh, well, you”d have to be expecting a certain date format” quicker than you can say “ISO 8601.”
<slaps forehead>
And there’s the problem – I don’t want to have to build in a switch for every known date format. If partner XYZ wants to enter a non-standard format (“-1 week 2 days 4 hours 2 seconds”?), we shouldn’t need to refactor the code to parse this.
Now. A disclaimer. I am not a JAVA programmer. I consider myself an intermediate PHP programmer. Programming is not my job nor is it my dream to become a full-time developer.
However, I’ve done my googleing, and I’ve asked 4 different JAVA experts who all agree it can’t be done as cleanly as strototime.
That being said? Booooo to you Java. Boooooo.
Writer. Musician. Adventurer. Nerd.
Purveyor of GIFs and dad jokes.