-
-
Save JeffreyWay/2889230 to your computer and use it in GitHub Desktop.
// Fun little quiz | |
Assuming this string: "January 5th, 2012" | |
In the shortest amount of code possible, place: | |
- 'January' within a $month variable | |
- '5th' within a $day variable | |
- '2012' within a $year variable. | |
See if you can accomplish this with one line of code. (And no snarky $month = 'January' answers. | |
Assume that the format always stays the same, but month, day, and year values can change with each referesh. |
Was thinking about doing a fun little quiz/riddle each day. Nothing that will require more than a few minutes.
Do it!
Ironically, this is almost exactly (minus the comma) one of the examples on the PHP manual's sscanf page. :D
Nice quiz. Just know it late :(
@thecrypticace Haha good spot! I still think the preg_split method looks alot nicer than sscanf() even though it's a bit longer (6 chars) !
@JeffreyWay Thanks, didn't know about it. What about non-space separators? Try e.g.
sscanf('2012-25-12', '%d-%d-%d', $y, $m, $d)
It doesn't work. Instead, I'd need:
sscanf('2012-25-12', '%[^-]-%[^-]-%[^-]', $year, $month, $day)
when
list($year, $month, $day) = explode('-' , '2012-25-12');
is ok. Or am I missing something in the sscanf syntax?
// array
date_parse(trim("January 5th, 2012",','));
//vars
extract(date_parse(trim("January 5th, 2012",',')));
Of course, my calendar has 30 months of 12 days each :D
@topdown, what is the point of the trim()
since there are no leading/following commas?
Combining @letsallplaygolf with @thecrypticace I get:
extract(date_parse(date('F jS Y')));
@xeoncross trim is stripping the comma
trim has an optional parameter for specifying a character list http://php.net/manual/en/function.trim.php
Wasn't thinking about date_parse already handling it.
@topdown I think @xeoncross was pointing out that trim() only trims characters from the beginning/end of a string, not the middle. So trim isn't required in your code :)
@JeffreyWay Nice teaser though. I didn't know about the sscanf() function.
Meh, you win @JeffreyWay, new quiz!