Given a date string in the format m-d-Y (e.g, “12-09-2011”) how can I display it in the format “m/d/Y” WITHOUT resorting to regular expressions.
Just in case you missed it: without using regular expressions.
I would prefer to use DateTime::createFromFormat() for this problem, but the server I am using currently doesn’t have 5.3.0 on it.
I’d also prefer using date('m/d/Y', strtotime("12-09-2011")) but strtotime() doesn’t recognize that format properly. It confuses the day and month.
No need for heavy artillery. Since you’re not rearranging the date components, a simple string search/replace will do:
If you WERE rearrangine things, e.g. durning your m-d-Y into d/m/y, then you’d need something a bit heavier: