3

I have a field in my MySQL table fecha as datetime, to which I want to apply the appropriate format and then get the difference using diffForHumans but it shows me error

"message": "The separation symbol could not be found\r\nThe separation symbol could not be found\r\nData missing", "exception": "InvalidArgumentException", "file": "C:\laragon\www\appcolegio\vendor\nesbot\carbon\src\Carbon\Carbon.php", "line": 582,

I have this in my class Visita

public function getFechaAttribute($value)
{

    return Carbon::createFromFormat('d/m/Y H:i:s',strtotime($value))
        ->timezone('America/Lima');
}

public function getDiffAttribute()
{
    $fx = $this->fecha;
    return Carbon::createFromFormat('d/m/Y H:i:s',strtotime($fx))
            ->timezone('America/Lima')
            ->diffForHumans();
}

In my view I want to show these two values, on the one hand the current date with the corresponding timezone and on the other the difference with the corresponding timezone

How can I achieve it? Use Laravel 5.5

5
  • Please check my answer below. If you still want to it manually, please show result of dd(strtotime($fx)); Commented Feb 4, 2018 at 18:09
  • Ok. What does dd($this->fech) show? Commented Feb 4, 2018 at 18:16
  • Ok. Put the dd($value) in the beginning of getFechaAttribute Commented Feb 4, 2018 at 18:19
  • What exactly does dd($value) show? Commented Feb 4, 2018 at 18:26
  • I've updated the answer. You're using the wrong format. Commented Feb 4, 2018 at 18:30

1 Answer 1

3

Change the format from:

d/m/Y H:i:s

To:

Y-m-d H:i:s

And remove the strtotime()

5
  • It works to a certain extent. The only thing it does not do is give it the form d/m/YYYY h:m:s to Fecha. remember I want to show two columns in my view
    – DarkFenix
    Commented Feb 4, 2018 at 18:11
  • @DarkFenix create another accessor but not for fecha. For example, public function getFechaStringAttribute and then use fechaString in the view. Or just do $model->fecha->format($customStringFormat) in the view. Commented Feb 4, 2018 at 18:13
  • It works, but not the format be lost? since I wish d/m/Y or d-m-Y , there is no way to show it like that?
    – DarkFenix
    Commented Feb 4, 2018 at 18:34
  • @DarkFenix you can create additional accessors for any format you want like getFechaYMDAttribute(). Commented Feb 4, 2018 at 18:35
  • @DarkFenix you already know how to create an additional accessor (the getDiffAttribute) and I've shown how to append it to JSON in another answer. Commented Feb 4, 2018 at 18:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.