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

hikups's avatar

Carbon

Hi

Im stuck on exacly the same problem as discussed here https://laracasts.com/discuss/channels/general-discussion/carbonparse-not-working-as-expected

In the laravel 5 fundamental videos, jeff shows how the timestamp of future posts can be reset to 00:00:00, instead of using the current time by using a Set Attribute on published_at in the Article Eloquent Model.

He converts the published_at to Carbon and uses Carbon::parse($date)

protected $dates = ['published_at'];
public function setPublishedAtAttribute($date)
    {
        $this->attributes['published_at'] = Carbon::parse($date);
    }

This works fine, in the database, post are recieving a 00:00:00 timestamp. But for some reason this doesn´t affect jeffs Articles he creates that are not future posts. They recieve a timestamp with the current time.

My problem is that this carbon::parse($date) is affecting all my new articles. So whem I post a new one for today they also get the 00:00:00 timestamp.

In my form view the published_at input field looks like this

<div class="form-group">
    {!! Form::label('published_at', 'Publish On:') !!}
    {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
</div>

my controller

 public function index()
    {
        $articles = Article::latest('published_at')->published()->get();
        return view('articles.index', compact('articles'));
    }

Thanks for your reply

0 likes
28 replies
mstnorris's avatar

This is what my setPublishedAtAttribute method looks like:

 public function setPublishedAtAttribute($date)
{
    $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
    Carbon::createFromFormat('Y-m-d', $date);
}

Can I clarify whether you do or don't want the timestamp to be set to 00:00:00?

hikups's avatar

@mstnorris

I tried it out but this doesn't reset the time for future posts.

to be clear, the futureposts are getting a timestamp 00:00:00 when jeffs uses carbon::pase($date) but this doesn't seem to effect his posts that are with todays date, in my case it does.

mstnorris's avatar

@hikups that is the intended behaviour.

Today's posts have a time! The ones in the future don't. It is just how it works.

mstnorris's avatar

@hikups, use:

    public function setPublishedAtAttribute($date)
    {
        $this->attributes['published_at'] = Carbon::parse($date); // this doesn't set a time so it would equal 2015-06-19 00:00:00
    }
hikups's avatar

@mstnorris i know this is the intendent behaviour :)

that's what i´m trying to figure out, why it's not working

mstnorris's avatar

Can you show me your database migrations? Is your published_at field of type timestamp?

hikups's avatar

the code above you posted is the code i'm using, check first post

yes, published_at is a type of timestamp

public function up()
    {
        Schema::create('articles', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->timestamp('published_at');
        });
    }
mstnorris's avatar

@hikups, so just so I am clear on this:

  1. You create a new article and set the date to now (or don't set it at all so it defaults to now), then you get a full timestamp like 2015-06-19 17:35:49 ??

  2. When you set the date to be in the future... What happens?

hikups's avatar

Depends what i´m using in my SetPublishedAtAttribute. When i´m using

$this->attributes['published_at'] = Carbon::parse($date);

future and current date are all set to 00:00:00

when i´m using

$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);

all posts are getting the current time added, so future posts are getting the future date, wich is good, but with the current time, wich is not good

In his videoseries Jeff gives a solution for this by using Carbon::parse($date); but as mentioned before, this affects both in my case

mstnorris's avatar

@hikups show me your app\Http\Controllers\ArticlesController and the associated ArticleRequest too. So far you and I have the same code so something is going on in there.

On a side note, run "composer dump-autoload -o" as you might be using old versions of your classes`

ArticlesController Controller

public function store(ArticleRequest $request)
{
    $this->createArticle($request);
    flash()->success('Your article has been saved');
    return redirect('articles');
}

ArticleRequest Request

<?php namespace App\Http\Requests;

class ArticleRequest extends Request {

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'body' => 'required|min:20',
            'published_at' => 'required|date'
        ];
    }
}
hikups's avatar
<?php namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ArticleRequest;
use App\Tag;
use Carbon\Carbon;
use Auth;

class ArticlesController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth', ['only' => 'create']);
    }

    public function index()
    {
        $articles = Article::latest('published_at')->published()->get();
        return view('articles.index', compact('articles'));
    }

    public function show(Article $article)
    {
        return view('articles.show', compact('article'));
    }

    public function create()
    {
        $tags = Tag::lists('name', 'id');
        return view('articles.create', compact('tags'));
    }

    public function store(ArticleRequest $request)
       {
           $article = Auth::user()->articles()->create($request->all());
           $article->tags()->attach($request->input('tags'));
           flash()->overlay('your articles has been created','Good Job!');
           return redirect('articles');
       }


    public function edit (Article $article)
    {
        return view ('articles.edit', compact('article'));
    }

    public function update (Article $article , ArticleRequest $request)
    {
        $article->update($request->all());
        return redirect ('articles');
    }

}
<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class ArticleRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'body' => 'required',
            'published_at' => 'required|date'
        ];
    }

}
hikups's avatar

runned "composer dump-autoload -o" but nothing changed.

jekinney's avatar

Your migration for publish_at should be dateTime not time stamp.

mstnorris's avatar

@jekinney I don't think that is it as I have the same code as the lesson and I'm using:

$table->timestamp('published_at');
hikups's avatar

indeed, changed the code and did a migrate:refresh and still not succesfull.

hikups's avatar

I checked every file on github and all are the same. Driving me crazy !

@Uhanalainen did you find a solution for this?

Kryptonit3's avatar

you could add ->startOfDay() to the end to make it auto set the hour:min:sec to 00:00:00

hikups's avatar

This is not a solution , adding this doesn´t change anything to my problem

mstnorris's avatar

@hikups you must be missing something.

Are you on Windows or Mac; and are you using Homestead?

hikups's avatar

I´m currently running on an old Windows 7 with Wamp installed. This is the only computer i have for the moment.

jekinney's avatar

Weird as I also have never had the same issue with publishing future times in any of my apps for blogs, announcements etc.

My only other guess which doesn't make sense is PHP version your running. In practice no carbon instance will work though.

hikups's avatar

i'm running php 5.5.12 and wampserver 2.5

mstnorris's avatar

@hikups I assume in that case you're not using Laravel 5.1 (as it requires PHP >= 5.5.9).

May I suggest one thing (you may not like it). Watch the series again from scratch without working alongside it. I used to fall into the trap thinking it will take me twice as long if I have to watch it twice. However, it doesn't, if anything it is quicker to watch it once without working along, and then watch it a second time (you can already anticipate what Jeffrey is going to do which is a plus) but a bigger advantage is that if Jeffrey does make a mistake which he goes on to fix in later videos, you won't be stumped.

So, watch it again from the start without doing anything. Then start a new project, from the beginning and follow what he is doing. I am sure then that you will find the answer.

hikups's avatar
hikups
OP
Best Answer
Level 1

Before i created this thread, i reviewed all of the videos from the beginning till where i the error appears , without success. Just downloaded that github project in my local enviroment and got the same error. So this must be something to do how my system is set up. Thanks anyway, but i guess i spend allready to much time on this. Maybe in the future when i have more understanding of laravel i'll find the error

kira02's avatar

We have the same issues/problems regarding on this matter. I think the logic to resolve the problem is by using IF CONDITION. Although I did not know the proper syntax. The logic is when the article is publish in less than or equal (== )to the current time (Carbon ::now) the time should be in the ff format : 2016-04-21 10:27:33 else 2016-04-21 00:00:00 (Carbon::parse($date)) e.g

public function setPublishedAtAttribute($date){

    if(['published_at'],'<=',Carbon::now()){
           $this->attributes['published_at'] = Carbon:now();
        }
     $this->attributes['published_at'] = Carbon::parse($date);
}

Please or to participate in this conversation.