php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45972 Date escaping goes incorrectly
Submitted: 2008-09-02 11:37 UTC Modified: 2012-05-29 00:00 UTC
From: woeterman at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.2.6 OS: Linux Debian
Private report: No CVE-ID: None
 [2008-09-02 11:37 UTC] woeterman at gmail dot com
Description:
------------
When escaping with date("\t"), it shows up as nothing instead of 't'.
It seems to work fine with other characters, such as \a

Reproduce code:
---------------
<?php
echo "1: ". date("jS \of F Y H:i:s"). "<br />"; 

echo "2: ". date("jS \of F Y at H:i:s"). "<br />";

echo "3: ". date("jS \of F Y \at H:i:s"). "<br />";

echo "4: ". date("jS \of F Y \a\t H:i:s"). "<br />";

echo "5: ". date("jS \of F Y \t H:i:s"). "<br />";
?>

Expected result:
----------------
1: 2nd of September 2008 13:36:57
2: 2nd of September 2008 pm30 13:36:57
3: 2nd of September 2008 a30 13:36:57
4: 2nd of September 2008 at 13:36:57
5: 2nd of September 2008 t 13:36:57

Actual result:
--------------
1: 2nd of September 2008 13:36:57
2: 2nd of September 2008 pm30 13:36:57
3: 2nd of September 2008 a30 13:36:57
4: 2nd of September 2008 a 13:36:57
5: 2nd of September 2008 13:36:57

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-02 11:46 UTC] derick@php.net
That's because normal string escape rules still apply, and \t is the tab character.
 [2012-05-28 23:57 UTC] dillydadally at yahoo dot com
I whole-heartedly disagree that this is not a bug, as does the PHP.net 
documentation itself!  If you look at the documentation, "\t" is used all over 
in the examples to represent how to type the letter t. For example, these are 
copied from the documentation:

$today = date('\i\t \i\s \t\h\e jS \d\a\y.');
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');

The backslash in the tab character should have to be escaped, so the tab 
character should be \\\t.  What's worse is this is not expected behavior and 
there is no solution or note posted in the php documentation, leaving many 
people confused.
 [2012-05-29 00:00 UTC] rasmus@php.net
dillydadally I think you missed the fact that the original report had double-
quoted strings. Your version with single-quoted strings works as you would 
expect.
eg.

php > echo date('H:m:s \m \i\s\ \m\o\n\t\h');
16:05:40 m is month
 [2012-05-29 00:26 UTC] dillydadally at yahoo dot com
Ah, you're right!  The distinction completely slipped my mind!  

I think the documentation is commenting on this here:

"You can prevent a recognized character in the format string from being expanded 
by escaping it with a preceding backslash. If the character with a backslash is 
already a special sequence, you may need to also escape the backslash."

I took this to mean the backslash in a tab character would have to be escaped, 
but on a second review, I understand what it's saying.  It's a bit confusing.  
Perhaps an example in the documentation would help clear things up.  Or maybe 
I'm just wrong.  Anyway, if anyone hits this and wants to use double quotes, you 
can still do a t by typing \\t.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 07:01:32 2024 UTC