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

eddydan's avatar

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

0 likes
5 replies
Snapey's avatar

What is in the field? The exact string "[2025-07-01,2025-08-31]", complete with square brackets, or is it a json field?

eddydan's avatar

@Snapey "[2025-07-01,2025-08-31]", complete with square brackets

Rebwar's avatar
Rebwar
Best Answer
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

eddydan's avatar

@Rebwar Thank you. Though I didnt create an accessor but your steps works just fine when I manipulated it on the view blade template.

Please or to participate in this conversation.