34

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
  • 159
  • 5
  • 19
Aliya Kcx
  • 359
  • 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

81

You can use S letter as following:

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

Check manual.

hsz
  • 143,040
  • 58
  • 252
  • 308
  • 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
19

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,038
  • 26
  • 81
  • 147
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,278
  • 3
  • 26
  • 38
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
  • 895
  • 10
  • 14
4

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

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,115
  • 6
  • 25
  • 41
Karan Shaw
  • 886
  • 7
  • 10