What is in the field? The exact string "[2025-07-01,2025-08-31]", complete with square brackets, or is it a json field?
Jan 11, 2025
5
Level 1
How can I conver date [2025-07-01,2025-08-31] to Jul 1, 2025 to Aug 31, 2025
I have a date in my database table as [2025-07-01,2025-08-31] in my laravel application,
I want to display to users as Jul 1, 2025 to Aug 31, 2025.
How can I achieve this?
I am new in coding and would appreciate it if anyone can help me with this.. Thnak
Level 32
@eddydan You can create an accessor in your model like this:
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
class YourModel extends Model
{
protected function formattedDateRange(): Attribute
{
return Attribute::make(
get: function() {
// considering date_range is the column name in your database
if (!$this->date_range) {
return null;
}
$dates = Str::between($this->date_range, '[', ']');
[$start, $end] = explode(',', $dates);
$startDate = Carbon::parse($start)->format('M j, Y');
$endDate = Carbon::parse($end)->format('M j, Y');
return "$startDate to $endDate";
}
);
}
}
Now, when you access the formatted_date_range attribute, it will show:
{{ $yourModel->formatted_date_range }}
The output will be: Jul 1, 2025 to Aug 31, 2025
Please or to participate in this conversation.