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

davidebo's avatar

Convert string to date

Hello, i would like to save the date coming from an input type datetime-local in my published_at field in database,

I'm saving date like this : $post->published_at = $req->input('published_at'); $req is request. dd( $req->input('published_at')) is "2024-02-05T14:04".

The input is this: <input type="datetime-local" name="published_at" class="form-control" value="2024-02-05 13:04:00" id="published_at">

When i try to show my date in frontend like this : {{$post->published_at->translatedFormat($format)}} it tells me is Call to a member function translatedFormat() on string . $locale = app()->getLocale(); $format = $locale === 'it' ? 'F, Y' : 'F, Y';

Someone can helps ?

0 likes
9 replies
LaryAI's avatar
Level 58

It seems like the issue you're encountering is due to the fact that the published_at attribute on your $post object is being treated as a string rather than a date object. To resolve this, you need to ensure that the published_at field is cast to a date object in your Eloquent model. Here's how you can do that:

First, make sure that your Post model has the published_at field listed in the $dates property or use the $casts property to cast it as a date. This will automatically convert the string to a Carbon instance, which is Laravel's default date handling package.

Here's an example of how to do this in your Post model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class Post extends Model
{
    // If you're using Laravel 7 or below, use the $dates property:
    protected $dates = ['published_at'];

    // If you're using Laravel 8 or above, you can use the $casts property:
    protected $casts = [
        'published_at' => 'datetime',
    ];

    // If you want to use a custom date format when casting, you can define a mutator:
    protected function publishedAt(): Attribute
    {
        return new Attribute(
            get: fn ($value) => $value ? Carbon\Carbon::parse($value) : null,
            set: fn ($value) => $value ? Carbon\Carbon::createFromFormat('Y-m-d\TH:i', $value) : null
        );
    }
}

When you're saving the date to the database, you can use the createFromFormat method from the Carbon class to create a Carbon instance from the input string:

use Carbon\Carbon;

$post->published_at = Carbon::createFromFormat('Y-m-d\TH:i', $req->input('published_at'));
$post->save();

Now, when you retrieve the published_at field from the database, it will be a Carbon instance, and you can use the translatedFormat method on it:

$locale = app()->getLocale();
$format = $locale === 'it' ? 'F, Y' : 'F, Y';
echo $post->published_at->translatedFormat($format);

This should resolve the error you're seeing and allow you to format the date as needed for display in the frontend. Remember to import the Carbon class at the top of your PHP file if you're using it directly:

use Carbon\Carbon;

Make sure to adjust the date format string in createFromFormat and translatedFormat to match the format you're expecting from the input and the format you want to display, respectively.

1 like
martinbean's avatar

@davidebo You’ve seen the format a datetime-local input gives you. It’s the date, a literal “T” character, and then the time (hours and minutes). As a PHP date expression, it would look like this: Y-m-d\TH:i.

You therefore need to format the value with the same format in order to set the value of a datetime-local input:

<input type="datetime-local" name="published_at" value="{{ old('published_at', $model->published_at->format('Y-m-d\TH:i')) }}">

If you get an error about trying to call format on a string, then you need to cast your published_at column in your model:

class Foo extends Model
{
    protected $casts = [
        'published_at' => 'datetime',
    ];
}

As an aside, you can get values from the request as a well-formed date object like this:

$publishedAt = $request->date('published_at');

This will get the submitted published_at value and parse it into a Carbon instance.

1 like
davidebo's avatar

@martinbean Ok thank you, so if i want to show my date in frontend as Month Year, so February 2024 for example, but i receive this format Y-m-d\TH:i from the form, can i save my date like this : $post->published_at = Carbon::createFromFormat('f-Y', $req->input('published_at')); or as you said $publishedAt = $request->date('published_at'); ?

davidebo's avatar

@martinbean Or is this the correct way : $post->published_at = Carbon::createFromFormat('Y-m-d\TH:i', $req->input('published_at')); because it has to be the same of what i reveive from form ?

Snapey's avatar

@davidebo if you are using the standard datetime input field, then you cannot have any control how it is displayed to the user. This is in their control and will be displayed according to their locale and user preferences. You should pass the existing value to the field in the Y-m-d\TH:i format

When saving the datetime, you need to use the native format of your database and then convert the saved value only at presentation.

1 like
davidebo's avatar

@Snapey i think i understand, anyway in database is saved like this 2024-02-05 11:04:00 datetime type

Snapey's avatar

@davidebo this is correct format to exchange date time with the database. If you cast as datetime in the model then it will be automatically converted to Carbon instances which you can format as you like in the view.

1 like
davidebo's avatar

ok thank you, i solved , i missed the cast as date in the model , and then i saved date in this way Carbon::createFromFormat('Y-m-d\TH:i', $req->input('published_at'))

Please or to participate in this conversation.