PHP

Using Text Within date()

date('\To\d\a\y \i\s \t\he jS \d\a\y of F')

Imagine you want to output the current date (or a specific date) and use custom strings in it. The code could look like a mess if you are trying it like this:

<?php
  echo 'Today is the ' . date('jS') . ' day of the
    month ' . date('F');
?>

Using Ordinary Characters in date() (date.php)

<?php
  echo date('\To\d\a\y \i\s \t\he jS \d\a\y of F');
?>

The output of this script is something like this, depending on the current date:

Today is the 3rd day of the month May

The behavior of date() is the following: All characters within the first parameter that bear a special meaning (for example, formatting symbols) get replaced by the appropriate values. All other characters, however, remain unchanged. If a character is escaped using the backslash character (\), it is returned verbatim. So, the code at theF beginning of This shows a new version of the code that only uses one call to date().

If you are using double quotes instead of single quotes, you might get into trouble when escaping certain characters within date(). In the previous example, escaping the n would be done with \n, which (within double quotes) gets replaced by the newline character.

by BrainBellupdated
Advertisement: