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

MarlonV's avatar

Covert date with pluck

It is possible to covert a date with a pluck?

{{ implode(', ', $child->events()->get()->pluck(\Carbon\Carbon::parse($event->start_date)->format('d:m:Y'))->toArray()) }}

The error i get:

DateTime::__construct(): Failed to parse time string (start_date) at position 0 (s): The timezone could not be found in the database
0 likes
4 replies
JohnBraun's avatar
$dates = $child->events()->pluck('start_date');

That will give you a collection of the 'start_date' properties of your events.

If you make sure to 'tell Laravel' to interpret them as dates in you Event.php model, the 'start_date' property will automatically be converted into a Carbon instance.

// Event.php
public $dates = ['start_date'];
MarlonV's avatar

Thanks for your comment John,

I have asked my question incorrectly, I want to give the date a different format, now the times comes with.

Thanks in advance

JohnBraun's avatar

@MARLONV - Can you try to perform a map operation on the dates collection?

$dates = $child->events()->pluck('start_date');

$formattedDates = $dates->map(function ($date) {
   return $date->format('d:m:Y');
});

Now your $formattedDates will contain a collection of dates that follow the appropriate format. If you want to convert it to an array, just chain ->toArray():

$formattedDates = $dates->map(function ($date) {
   return $date->format('d:m:Y');
})->toArray();
bobbybouwmann's avatar
Level 88

You can also set the default date format on the model itself

class Event extends Model
{
    protected $dateFormat = 'd:m:Y';

    protected $dates = ['start_date'];
}
1 like

Please or to participate in this conversation.