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

Uhanalainen's avatar

Carbon::parse not working as expected

I'm watching the Laravel 5 fundamentals -series, and it's been great so far, but I'm stuck on lesson 11. Jeffrey uses Carbon::parse($date) to get a carbon instance of the date given in the form, and in the video, it works like so:

If the day is the current day, the time will be the current time. Otherwise, if the given date is in the future, the publication date will be the given date, BUT the time will be 00:00:00.

I've been toying around this thing for hours, but I can't seem to grasp why it's not working, and judging by the comments, I'm not alone. I've been trying to move on, even, but this bugs the heck out of me, and I'm reluctant to move on for fear of other, more serious complications rising up in front of me.

I actually sort of got it working with Carbon::createFromFormat('Y-m-d', $date) but with that, the time will always be current time even if the date is in the future, which was the reason Jeffrey changed it in the video to Carbon::parse.

I'm thinking of making a subscription (love the quality vids here!) but I need to know that I can get help if I get stuck with them for whatever reason, so any help in this issue would be great.

Thanks in advance.

0 likes
6 replies
Uhanalainen's avatar

But you can't give parse any more parameters than one, $date, which is a string, and doesn't hold the time information, which can be seen if you dd($date). So where does Jeffrey's app get the timestamp from? Because it's obviously working on his machine. And that's with Carbon::parse.

The video I'm talking about is this one:

https://laracasts.com/series/laravel-5-fundamentals/episodes/11

bobbybouwmann's avatar

He is doing two different things there (let's assume the date is today and my timezone)

His first attempt is without carbon and he uses the date input so it will only give back the date. If you want the time with it, just change the input type to datetime

{!! Form::input('datetime', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}

Back to Carbon, Carbon will get the time for you since it's available in the system ;)

Carbon::createFromFormat('Y-m-d', $date); // 2015-02-27 10:53:00

Finally a date with a time that's set to zero

Carbon::parse($date); // 2015-02-27 00:00:00
JarekTkaczyk's avatar
Level 53

@Uhanalainen I'm not watching the video and not referring to it, but I suppose you don't know how parse can be used.

You pass any string representation of a date/timestamp to the method:

[1] > Carbon::parse('-1 day');
// object(Carbon\Carbon)(
//   'date' => '2015-02-26 11:18:58',
//   'timezone_type' => 3,
//   'timezone' => 'Europe/Warsaw'
// )
[2] > Carbon::parse('tomorrow');
// object(Carbon\Carbon)(
//   'date' => '2015-02-28 00:00:00',
//   'timezone_type' => 3,
//   'timezone' => 'Europe/Warsaw'
// )
[3] > Carbon::parse('1 year 1 month 22 day 5 hour');
// object(Carbon\Carbon)(
//   'date' => '2016-04-18 16:19:17',
//   'timezone_type' => 3,
//   'timezone' => 'Europe/Warsaw'
// )
[4] > Carbon::parse('-1 year 1 month 22 day 5 hour');
// object(Carbon\Carbon)(
//   'date' => '2014-04-18 16:19:23',
//   'timezone_type' => 3,
//   'timezone' => 'Europe/Warsaw'
// )
[5] > Carbon::parse('2015-02-20 12:55:11');
// object(Carbon\Carbon)(
//   'date' => '2015-02-20 12:55:11',
//   'timezone_type' => 3,
//   'timezone' => 'Europe/Warsaw'
// )
[6] > Carbon::parse('2015-02-20');
// object(Carbon\Carbon)(
//   'date' => '2015-02-20 00:00:00',
//   'timezone_type' => 3,
//   'timezone' => 'Europe/Warsaw'
// )
mstnorris's avatar

@hikups that's ok, please don't apologise otherwise I'll feel bad, just wanted to point out otherwise it can be confusing. Do you want to edit what you just wrote, copy & paste it into a new question and I'll try my best to help.

Also, if you wouldn't mind, could you add to it the code form your view, your model and your controller that handle the saving of the posts.

Thanks.

Please or to participate in this conversation.