Gagan
Gagan

Reputation: 5656

Get month in string

I have a created_at datetime string. What I would like to do is to display the date and the month (in string format). so for example if I have a date like

15.10.2014

I would like to have

October 15

I would like to avoid if and switch case statements. I am using Carbon DateTime library.

Upvotes: 2

Views: 2534

Answers (4)

FBarawi
FBarawi

Reputation: 51

You can customize the date into many formats according to the read.me file of the project Here what I have found in the read.me

     $dt = Carbon::create(1975, 12, 25, 14, 15, 16);

     var_dump($dt->toDateTimeString() == $dt);          // bool(true) => uses __toString()
     echo $dt->toDateString();                          // 1975-12-25
     echo $dt->toFormattedDateString();                 // Dec 25, 1975
     echo $dt->toTimeString();                          // 14:15:16
     echo $dt->toDateTimeString();                      // 1975-12-25 14:15:16
     echo $dt->toDayDateTimeString();                   // Thu, Dec 25, 1975 2:15 PM

     // ... of course format() is still available
     echo $dt->format('l jS \\of F Y h:i:s A');         // Thursday 25th of December 1975 02:15:16 PM'


     Carbon::setToStringFormat('jS \o\f F, Y g:i:s a');
     echo $dt;                                          // 25th of December, 1975 2:15:16 pm

     Carbon::resetToStringFormat();
     echo $dt;                                          // 1975-12-25 14:15:16

You will find more information in the read.me file in the project page on github from here

Upvotes: 0

Havelock
Havelock

Reputation: 6968

And here is the DateTime approach (Carbon extends the DateTime class)

$date = DateTime::createFromFormat('d.m.Y', '15.10.2014');
$yourString = $date->format('F d');

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

Better to use datetime

$date = new DateTime('15.10.2014');
echo $date->format('F d'); //October 15

Upvotes: 0

Trushali
Trushali

Reputation: 76

echo date('F d',strtotime('15.10.2014'));

Upvotes: 6

Related Questions