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

SaraBalliana's avatar

Problem with format date in laravel

Hello guys :) im trying to format my data for change the format in my db from y-m-d to d-m-y but i need someone more expert because i have no idea what is the problem, and not sure that i make the right thing this is my test

when i try in my app i have this error

the application works but i have to change the format this is my db with the appointments

0 likes
10 replies
Snapey's avatar

Please dont post pictures of code.

Leave everything in Carbon. Eloquent will do the conversion for you.

$start = Carbon::createFromFormat('Y-m-d H:i', $data['start']);
$end = $start->copy()->addMinutes($service->duration);

$appointment = $this->create([
    'start' = $start,
    'end' = $end,
    'client_id' = Auth::id()
]);
1 like
tykus's avatar

Whenever you use the Carbon format method; you get a string.

Do you want $data['start'] as a d-m-Y formatted string or as a DateTime string? Why does the create method not accept Carb on instances?

Randy_Johnson's avatar

Just grab the date from the db and $today = date("F j, Y, g:i a", $dateFromDB);

And if you need it another way just check below.


<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>

Please or to participate in this conversation.