Skip to content
Advertisement

PHP – Date Format to include a string

I am trying to format a PHP date to be of the following format:

Wednesday 3rd November 2021 at 11:01am

My code is below:
$date = new DateTime($dateOfChange);
$date = $date->format(‘l jS F Y “at” g:ia’);

I have tried:
$date = $date->format(‘l jS F Y “at” g:ia’); // displays: “am01”
$date = $date->format(‘l jS F Y at g:ia’); // displays: am01

I can’t see a relevant section in the documentation (https://www.php.net/manual/en/datetime.format.php)

Advertisement

Answer

This should do the trick…

$date->format('l jS F Y at g:ia');

You need to escape characters that are not intended to be used for date values.

Note that if you’re using " instead of ' for your format string you’ll need to double escape n, t and r characters as PHP will interpret them as newlines, tabs etc…

For example:

$date->format("l jS F Y a\t g:ia");
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement