PHP date with span tags in

Hi there,
I am trying to wrap some date outputs in a span tag, but I am getting some weird things being outputted.

This is what I have:

$start_date_short = date('<span class="day">D</span><span class="day-number">jS</span>', $timestampStart);

and using this to echo it:

<?php echo $start_date_short; ?>

However, it is showing this:

image

Any ideas why this is?

I’ve tried to escape the span classes but don’t think it needed it as it still outputs all of the above.

the purpose of the date() function is to return a formatted date/time, not to produce html markup. every character between the opening ’ and closing ’ that that is a valid format character is being replaced with the formatted value. to get this to work the way you are trying to use, you would need to escape every character that is a valid format character.

i recommend that you use printf() or sprintf() to build the output, and call the date() function for each different section of formatted date/time that you want to put into the output.

Here is a basic example…

index.php

<?php 
 $dt = new DateTime();
 $content = 
 ' <span>'.$dt->format('D').',</span>
 <span>'.date('j').'</span>
 <span>'.date('F').',</span>
 <span>'.date('Y').'</span>';
?>
<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>PHP Date</title>

<link rel="stylesheet" href="screen.css" media="screen">

</head>
<body> 

<div id="date">
<?php echo $content;?>
</div>

</body>
</html>

screen.css

body {
    font: normal 1em / 1.5  arial, helvetica, sans-serif;
 }
#date {
	font-size: 1.25em;
	text-align: center;
}

To be precise to what @mabismad is explaining, you say:

PHP sees:

Output "<"
Then the number of seconds, 2-digits with leading zero.
Then "p"
Then either am or pm
Then the numeric value of the month.
Then a space
Then the ISO-8601 date.
Then the full-word day of the week.
Then either am or pm
Then the number of seconds, 2-digits with leading zero, twice
....

I got as far as translating <span class before stopping there.
The following letters are translated in a date format:
aABceFgGhHiILmMnoOpPrstTuUvxXyYZ