Problem with dates

Hello everyone,

I am following the tutorials from laravel and I am trying to switch between 2 functions that I want to call.
one is for when a page needs to be shows tomorrow for example. ( then I want to do : Carbon::parse ).
And one if a page needs to be shown today and then I would do : Carbon::now().

Here is my code :

    /**
     * setPublishedAtAttributes
     * @param $data
     * @return void
     */
    
    public function setPublishedAtAttribute($date) 
    {

        if($date !== Carbon::now()->toDateString()) {
            $this->attributes['published_at'] = Carbon::parse($date);    
        }
        $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
    }

How can I achieve this properly.

What value is in $date?

Scott

$date would be a selected date or the date off today.
For example today $date would be : 2015-11-22.
And tomorrow would be : 2015-11-23

   /**
     * setPublishedAtAttributes
     * @param $date
     * @return void
     */
    public function setPublishedAtAttribute($date) 
    {
        if($date !== Carbon::now()->toDateString()) {
            $this->attributes['published_at'] = Carbon::parse($date);    
        }else{
          $this->attributes['published_at'] = $date;
        }
    }

???

Scott

Give me a few while I try that out.

@s_molinari Thank you it actually worked.
that was stupid that I missed a simple else…

Well thank you very much !

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.