John Hoffman
John Hoffman

Reputation: 18627

Why does my date format function only work when I use single quotes?

Here is date format function in PHP.

function formatMyDateNicely($strTimeString) {
    return date('F j, y \a\t g:iA', strtotime($strTimeString));   
}

It outputs a date akin to "April 23, 12 at 12:01PM".

However, this function, which uses double quotes, does not work.

function formatMyDateNicely($strTimeString) {
    return date("F j, y \a\t g:iA", strtotime($strTimeString));   
}

This function outputs a date akin to "April 23, 12 a 12:01PM".

Why does changing the type of quotes matter?

Upvotes: 0

Views: 94

Answers (2)

Anthony
Anthony

Reputation: 37065

It's treating the \t as a tab character because of the double quotes. Another alternative would be to "double escape" the t:

return date("F j, y \a\\t g:iA", strtotime($strTimeString)); 

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799150

\t is a tab character. Single quotes inhibit interpretation of escape sequences.

Upvotes: 6

Related Questions