Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created June 7, 2012 14:56
Show Gist options
  • Save JeffreyWay/2889230 to your computer and use it in GitHub Desktop.
Save JeffreyWay/2889230 to your computer and use it in GitHub Desktop.
Simple PHP Quiz
// 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.
@montes
Copy link

montes commented Jun 7, 2012

shit! same here... =)

list($month, $day, $year) = explode(' ', str_replace(',', '', 'January 5th, 2012'));

@spacebeers
Copy link

  1. list($month, $day, $year) = explode(' ', date('F jS Y'));
  2. list($month, $day, $year) = preg_split('/[\s,]+/', 'January 5th, 2012');
  3. list($month, $day, $year) = preg_split('/[\s,]+/', date('F jS Y'));

Does that make @letsallplaygolf the winner or can it get even shorter?

@thecrypticace
Copy link

This one does not fit exact criteria, however for numerical answers only (year: 2012, month: 1, day: 5), this is the shortest I could make it:

extract(date_parse("January 5th, 2012"));

@JeffreyWay
Copy link
Author

Stol's got the first solution!

@m4tthumphrey
Copy link

@Letsallplaygolf isn't correct, he missed the comma ;)

@spacebeers
Copy link

@m4tthumphrey - Good spot. Must learn regular expressions.

@JeffreyWay
Copy link
Author

There's a slightly shorter solution than Stol's. Anyone?

@m4tthumphrey
Copy link

@JeffreyWay I assume

list($month,$day,$year)=preg_split("/[ ,]+/",'January 5th, 2012'); 

doesn't count ;)

@spacebeers
Copy link

list($month,$day,$year)=preg_split('/[\s,]+/', 'January 5th, 2012');

Is slightly shorter but I'm sure that's not what you mean

list($month, $day, $year) = preg_split('/[\s,]+/', 'January 5th, 2012');

@JeffreyWay
Copy link
Author

hehe - no.

@m4tthumphrey
Copy link

@spacebeers @stol's is shorter as he uses ' ' instead of '\s'

@JeffreyWay
Copy link
Author

@stol - That wasn't mine.

@JeffreyWay
Copy link
Author

Okay - so I came up with two answers. The first was the exact same as Stol's, and the other was using the sscanf function, which a lot of people don't know about. Think of it as the inverse of sprintf.

sscanf('January 5th, 2012', '%s %[^,], %d', $month, $day, $year);

@m4tthumphrey
Copy link

@JeffreyWay Haha nice! There's blatantly a shorter way using another hidden PHP gem!

@spacebeers
Copy link

@JeffreyWay - strangely enough my internet history seems to think I've read up about sscanf before even though I don't recall doing so. More challenges!!!

@JeffreyWay
Copy link
Author

@m4tthumphrey - Do tell! :)

Copy link

ghost commented Jun 7, 2012

@m4tthumphrey Smelly comma :) I must have mis-understood the problem a wee bit.

@m4tthumphrey
Copy link

I'm hunting!

@m4tthumphrey
Copy link

Meh, you win @JeffreyWay, new quiz!

@JeffreyWay
Copy link
Author

Was thinking about doing a fun little quiz/riddle each day. Nothing that will require more than a few minutes.

@m4tthumphrey
Copy link

Do it!

@thecrypticace
Copy link

Ironically, this is almost exactly (minus the comma) one of the examples on the PHP manual's sscanf page. :D

@rilwis
Copy link

rilwis commented Jun 7, 2012

Nice quiz. Just know it late :(

@m4tthumphrey
Copy link

@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) !

@abennouna
Copy link

@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?

@topdown
Copy link

topdown commented Jun 7, 2012

// array
date_parse(trim("January 5th, 2012",','));
//vars
extract(date_parse(trim("January 5th, 2012",',')));

@abennouna
Copy link

Of course, my calendar has 30 months of 12 days each :D

@xeoncross
Copy link

@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')));

@topdown
Copy link

topdown commented Jun 7, 2012

@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.

@stetodd
Copy link

stetodd commented Jun 7, 2012

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment