Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting date, ordinal suffix for the day of the month

Tags:

date

php

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?

like image 542
user3754277 Avatar asked Jun 18 '14 23:06

user3754277


2 Answers

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.

like image 179
dave Avatar answered Sep 30 '22 13:09

dave


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

Just check the documentation.

like image 21
Havenard Avatar answered Sep 30 '22 14:09

Havenard