Are you enjoying the extensions? Did you like the support? Help others decide.

Leave a review
27Mar2023
Information
Print

You can skin a date formatted with PHP

Information
First published December 11, 2017
15991 hits -
 

The PHP date format is very powerful and, although most people understand how to parse a date with the date() PHP function, it is less known that one can include HTML tags into it. Why would you really need to add HTML tags into a parsed date? It will allow you to have a handle on its content, therefore enabling its styling through CSS.

Here is an example on how to incorporate and 'escape' the <sup> HTML tag to skin the day of a date:
<\s\u\p>d</\s\u\p> F Y

The result will be:
<sup>11</sup> December 2017

Now, it is just a matter of adding a little CSS to style the resulting output:

sup {
    display: inline-block;
    top: 0;
    padding: 15px 8px;
    background-color: #5091cd;
    color: #fff;
    font-size: 1.2em;
}

11 December 2017

Note In some cases, you may need to add an extra backslash, for instance if you want to add the <br /> HTML tag, since \b and \r are meaningful escape sequences in double-quoted strings.

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.

Reference: The PHP Date Function