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

janisozolins's avatar

Storing user's date of birth as a timestamp object?

Currently I am retrieving user date of birth in a YYYY-MM-DD format and it is stored in a database as a string. However, I would like to store it similar to created_at, updated_at and deleted_at timestamps, so that I can use ->format() function in my Blade templates.

What would be the right approach to do so?

0 likes
5 replies
balakan's avatar

If I understood correctly, you need to change your table column type to timestamp instead of varchar, and save it in given format. It will be saved as timestamp, but hours, minutes and seconds will be added as zeros YYYY-MM-DD HH:MM:SS or 2017-03-25 00:00:00.

Or, if you want to stick with string, take a look at Carbon and how to get Carbon date representation from a date string.

Hope it helps.

jlrdw's avatar

What if it's an older user? Are you running in Maxdb sql mode? If not for a birtday, a date field is needed, unless you need time as well, then a datetime field.

Cronix's avatar

A birthdate is a date, not a date+time, so I'd use the date mysql datatype for that field. Then in your model add

protected $dates = [
        'created_at',
        'updated_at',
        'birth_date'
    ];

and it will automatically convert birth_date to a carbon instance so you can format() it like it does for created_at and updated_at like you're wanting.

It's really best to use appropriate datatypes and not do things like store dates as strings. There are a lot of powerful mysql features that work on dates that you lose when using strings. Like what if you wanted a list of everybody who has a birthday this month? SELECT * FROM table WHERE MONTH(birth_date) = 1 would give you all birthdays in January. If birth_date was a string column you'd have a lot more work to do.

1 like
bradbforbes's avatar

You can mark the attribute as a date by adding it to the $dates property of the model. Eloquent doesn't require it to be any specific type of SQL date type, but it will mutate it to a Carbon object as you're wanting it to be.

Snapey's avatar

Yes, use a date field so that you are not restricted to dates since 1970

Chances are that you will want to get the date from your form in a format that is friendly to your user.

You can use accessors and mutators to manage the date field.

Supposing your date comes from your form in the format 03 Mar 2017 then you can parse it in a mutator so that you can just send the date to the model.

    public function setBirthDateAttribute($value)
    {
        $this->attributes['birth_date'] = $this->parseDate($value);
    } 

    public function parseDate($date=null)
    {
        if(isset($date))
        {
            return Carbon::createFromFormat('d M Y',$date);
        }
        return null;
    }

The isset check allows you to throw the date field into the model and if the user has not provided a date then it won't throw an error.

Obviously I don't know what your field name is here.

Please or to participate in this conversation.