user3754277
user3754277

Reputation: 225

Formatting date, ordinal suffix for the day of the month

So if I use:

<?php echo date_format($date, "j M Y") ?>

I get a date in the following format: 5 Jan 1950.

However, what I want is something along the lines of: 5th Jan 1950

How would I go about adding the extra th?

Upvotes: 6

Views: 11409

Answers (2)

dave
dave

Reputation: 64695

Look at the formats here http://www.php.net/manual/en/function.date.php, but

<?php echo date_format($date, "jS M Y") ?><br>

For international dates, I guess you would do something like:

$ordinal = new NumberFormatter($locale, NumberFormatter::ORDINAL);
$ordinal = $ordinal->format(date_format($date, "j"));
$pattern = "d'{$ordinal}' MMM yyyy";
$dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::FULL, $timezone, IntlDateFormatter::GREGORIAN);
$dateFormatter->setPattern($pattern);
$dateFormatter->format($date->getTimestamp());

The above is untested but it seems like it would work.

Upvotes: 20

Havenard
Havenard

Reputation: 27914

echo date_format($date, "jS M Y");

Just check the documentation.

Upvotes: 9

Related Questions