Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

Is that possible to use carbon parse only month and year to string without day?

I have a value

12/2017

I want convert it to December 2017. Is that possible to do so using Carbon?

0 likes
4 replies
bobbybouwmann's avatar
Level 88

Yeah you can, but you need to pass a date anyway! So what you can do is something like this:

$dateMonthArray = explode('/', '12/2017');
$month = $dateMonthArray[0];
$year = $dateMonthArray[1];

$date = Carbon::createFromDate($year, $month, 1);

Note: You can leave the day parameter (the third one) off, but than it will pick the current day. Just make sure you know that because it might give you unexpected results.

You can also reset the time and date if you wish to the start day of the month.

$date = Carbon::createFromDate($year, $month)->startOfMonth();
6 likes
webxion's avatar

@bobbybouwmann I know this thread is over 7 years old, but I had the same problem today and ended up here.

I wanted to point out that the first approach works fine, but the second approach with ->startOfMonth() returns an incorrect date:

Carbon::setTestNow('2025-03-31');
$date = Carbon::createFromDate(2025, 2)->startOfMonth();

dd($date->toDateString()); // 2025-03-01

No offense. I just wanted to mention this in case anyone else ends up here looking for an answer.

My (not elegant) solution to this problem is as follows:

FormRequest:

public function rules(): array
{
  return [
    'report-month' => ['required', 'date_format:Y-m'],
  ];
}

Controller:

function myAction(FormRequest $request) 
{
  $date = $request->validated('report-month'); // <- string
  $reportDate = $this->dateStringToCarbon($date);

  $reportDate // <- Carbon
}


protected function dateStringToCarbon(string $date): Carbon
    {
        $date = sprintf('%s-%s', $date, '01'); // set to first day of month to avoid overlap issues

        return Carbon::createFromFormat('Y-m-d', $date);
    }





Crazylife's avatar

@bobbybouwmann By using createFromDate(), it will show me the result

2017-12-01 14:21:31

I am expected only show

December 2017
bobbybouwmann's avatar

Yeah, you need to format it as well. So something like this

$date = Carbon::createFromDate($year, $month, 1);

echo $date->format('F Y');
5 likes

Please or to participate in this conversation.