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

Eduardogrq's avatar

Display a date in a format and store it in other (Datepicker in Laravel)

Hi.

I'm working in laravel and I want to show in my Bootstrap Datepicker field a date in a format (dd/mm/yyyy) but I want to store it in format (yyyy/mm/dd).

are there some property in Datepicker to do that? this is my input:

<div class="form-group">
      <label for="fecha_nac"></label>
      <input type="text" class="form-control CalendarioDP" 
             name="fecha_nac" placeholder="Colocar fecha"
             value="{{ Carbon\Carbon::now()->formatLocalized('%d/%m/%Y') }}">
</div>

My Script:

<script>
    $('.CalendarioDP').datepicker({
        format: "dd/mm/yyyy",
        language: "es",
    });
</script>

When I let the format in script like this way, the date is showed it and stored it in a format (dd/mm/yyyy), but I want to stored it in (yyyy/mm/dd).

I have used "Torzer DateTime Mutator" and "protected $dates" but I want to know if there are exist a simple DaterPicker property to declaret just once in the script and not to specify each field that I want to Convert like in Torzer.

0 likes
3 replies
jlrdw's avatar

Just format again in code for storage, if mysql:

Example form SO article

$var = '20/04/2012';   // just example, use your variable
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));

Or study carbon and use that. https://carbon.nesbot.com/docs/

jaec86's avatar

Adding to @jlrdw answer you can use accesor and mutator in your model.

1 like
yordanovbg's avatar

    public function getFechaNacAttribute($value)
    {
        return $value ? Carbon::parse($value)->format('dd/mm/yyyy') : null;
    }

    public function setFechaNacAttribute($value)
    {
        if(empty($value)) {
            $this->attributes['fecha_nac'] = null;
        }else {
            $this->attributes['fecha_nac'] = Carbon::createFromFormat('dd/mm/yyyy', $value)->format('yyyy/mm/dd');
        }
    }

1 like

Please or to participate in this conversation.