37

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

jg2703
  • 169
  • 4
  • 20
Aliya Kcx
  • 389
  • 1
  • 3
  • 3
  • 1
    Possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Ivan Gabriele Jul 04 '16 at 12:36

7 Answers7

90

You can use S letter as following:

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

Check manual.

hsz
  • 148,279
  • 62
  • 259
  • 315
  • 2
    If you find my answer correct, mark the check under the answer's score. – hsz Nov 30 '12 at 08:00
  • 12
    Also better to use `j` if you want "1st" instead of "01st" appearing in your dates: `$new_date = date('jS F Y', strtotime($user_date));` – neave Oct 10 '14 at 12:00
  • `j<\s\u\p>S\s\u\p>` looks even better with `st`, `nd`, `rd` at the **top-right** of the day number – Teo Mihaila Aug 04 '22 at 09:57
21

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
PHP Ferrari
  • 15,754
  • 27
  • 83
  • 149
9
$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)

Avag Sargsyan
  • 2,437
  • 3
  • 28
  • 41
user9187674
  • 91
  • 1
  • 1
4
**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**
Nikit Barochiya
  • 971
  • 11
  • 14
3

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>');
3

You can do that :

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

$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

jcoppens
  • 5,306
  • 6
  • 27
  • 47
Karan Shaw
  • 1,206
  • 10
  • 11