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

mturner20's avatar

Carbon timeformat

I am using angular js to grab the time the user inputs into a form. And it returns weird like 2017-04-18T16:07:57Z. Now I am trying to use Carbon to put it in the right format of 16:07:57.

Here is my method of saving/posting the form

public function store(Request $request) {

    $event = new Event();
    $event->event_title = $request->input('event_title');
    $event->event_description = $request->input('event_description');
    $event->event_location = $request->input('event_location');
    $event->event_date = $request->input('event_date');

    $event->event_time = Carbon::now();
    $event->save();

    //return response()->json(['event'=>$event, 'user'=>$user], 201);
    return 'Event record successfully created';
}
0 likes
4 replies
jbowman99's avatar

from the docs:


$dt = Carbon::create(1975, 12, 25, 14, 15, 16);

var_dump($dt->toDateTimeString() == $dt);          // bool(true) => uses __toString()
echo $dt->toDateString();                          // 1975-12-25
echo $dt->toFormattedDateString();                 // Dec 25, 1975
echo $dt->toTimeString();                          // 14:15:16
echo $dt->toDateTimeString();                      // 1975-12-25 14:15:16
echo $dt->toDayDateTimeString();                   // Thu, Dec 25, 1975 2:15 PM

// ... of course format() is still available
echo $dt->format('l jS \\of F Y h:i:s A');         // Thursday 25th of December 1975 02:15:16 PM

add ->format('m-d-Y') or however you'd like the date to be formated Carbon::now()->format ...

or Carbon::now()->toDate()...

1 like
somnathsah's avatar
Level 3

Try this

Carbon::now()->format('h:i:s');
3 likes
mturner20's avatar

that works!! Instead of getting the current time how would I grab the time they are entering in the form? to time string??

jbowman99's avatar

@mturner20

what is your input for the time like?

$model->time_element = Carbon::createFromFormat('m/d/Y h:i a', $request->time_element);

this will let you create a carbon date from a passed in date/time input

Please or to participate in this conversation.