简体   繁体   中英

PHP Carbon formatting day in st, nd, rd, th format

I am using PHP Carbon to format the day. I get the day of the date like this.

$day = Carbon::parse($date)->format('d');

But I want to format that day something like this.

'01st', '2nd', '3rd', '4th'

How can I do that, please?

I'd recommend using DateTime class in PHP

$day = DateTime::createFromFormat('Y-m-d','2019-06-09')->format('jS');

//Or Using Existing Carbon Class

$day = Carbon::parse($date)->format('jS');

For More Info DateTimeFormats in PHP PHP Date Manual

You can format it with Carbon as below

Carbon::parse($date)->format('dS') // with preceding 0 ie 01st
Carbon::parse($date)->format('jS') // without preceding 0 ie 1st

for more See Carbon Documentation

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