Home > Software engineering >  PHP - Date Format to include a string
PHP - Date Format to include a string

Time:11-25

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)

CodePudding user response:

This should do the trick...

$date->format('l jS F Y \a\t 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");
  • Related