Aliya Kcx
Aliya Kcx

Reputation: 429

PHP date conversion dd [th/st/rd] / month / yyyy

I have my users entering the date in this format :- mm/dd/yyyy (11/21/2012)

My PHP script converts the date into the following format :- dd-Month-yyyy (21-November-2012)

I do this using :-

$new_date = date('d-F-Y', strtotime($user_date));

How can I have the date in this format :- 21st November 2012?

Thanks

Upvotes: 41

Views: 54562

Answers (7)

Pierre Alexandre
Pierre Alexandre

Reputation: 239

You can do that :

<?php echo date('dS M. Y');?>

Upvotes: 4

hsz
hsz

Reputation: 152216

You can use S letter as following:

$new_date = date('jS F Y', strtotime($user_date));

Check manual.

Upvotes: 92

Kiryamwibo Yenusu
Kiryamwibo Yenusu

Reputation: 176

You can use something like:

echo date('l, F jS');

Or even get a bit fancy with the HTML:

echo date('l, F j<\s\u\p>S</\s\u\p>');

Upvotes: 3

user9187674
user9187674

Reputation: 101

$new_date = date('jS F Y', strtotime($date));

S - English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th. Works well with j)

Upvotes: 10

Nikit Barochiya
Nikit Barochiya

Reputation: 1091

**My Date = 22-12-1992**
<?php
    $mydate = "22-12-1992";
    $newDate = date("d M Y", strtotime($mydate));
    $new_date = date('dS F Y', strtotime($newDate));
    echo $new_date;
?>

**OutPut = 22nd December 1992**

Upvotes: 4

Karan Shaw
Karan Shaw

Reputation: 1406

$date = date_create('09-22-2012');
echo $date->format('d S F Y');
by this code you will get your desire

for more you can also visit http://php.net/manual/en/datetime.formats.date.php

Upvotes: 0

PHP Ferrari
PHP Ferrari

Reputation: 15616

It will output as you expect

$my_date = '2016-01-01';

echo date('F jS, Y', strtotime($my_date));
# January 1st, 2016

while dS will also prepends 0

echo date('F dS, Y', strtotime($my_date));
# January 01st, 2016

Upvotes: 23

Related Questions