简体   繁体   中英

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

You can use S letter as following:

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

Check manual .

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
$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 )

**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**

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

You can do that:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM