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

gislig's avatar

Date and Time parsing

I am trying to run a function from a view, I am not sure if I should create the function in the model or the controller. What I am trying to do is to modify a string of number to date format which looks something like this 20160306124514.0Z and I created a function which looks like this

    public function change_ldap_date($value){
        return preg_replace("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).+/","$1-$2-$3 $4:$5:$6", $value);
    }

Now the view I use

$this->change_ldap_date($aplq_result['whencreated'][0])

But I always get an error

Call to undefined method Illuminate\View\Engines\CompilerEngine

Is there anyway around this, maybe using Carbon ?

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Do you care about the .0Z?

Carbon::createFromFormat($format, $time, $tz);

Your format is 'YmdHis.uZ', according to the format described here http://php.net/manual/en/datetime.createfromformat.php

public function ldapCarbon($value)
{
    return \Carbon\Carbon::createFromFormat('YmdHis.uZ',$value);
}

** edited

gislig's avatar

This function, should it be in the controller or the model.

<td>{{ $this->ldapCarbon($aplq_result['whencreated'][0]) }}</td>

If I use this line it shows an error, so I am not sure how to point to the function in the view?

It works if I use the carbon and the value straight to the view. Like this

<td>{{ \Carbon\Carbon::createFromFormat('YmdHis.uZ',$aplq_result['whencreated'][0]) }}</td>
Snapey's avatar

how is it stored in the model?

yes, I would get it from the model in carbon format and then ask carbon for the right format in the view.

gislig's avatar

I am not sure what you mean how it is stored in the model. I pretty noob in Laravel so I am trying to figure it out. As my understanding of model is that the model gets data and passes it to the controller where it is bundled someway and then passed to the view. I would figure out that the function should be in the controller and the view would then call the function to do something. But the problem is that I am not sure how to get to the function on the controller from the view i.e. would I use $this->ldapCarbon or should I call the function in some other means ? I would rather use function to do this because I am doing this multiple times

                <td>{{ \Carbon\Carbon::createFromFormat('YmdHis.uZ',$aplq_result['whencreated'][0]) }}</td>
                <td>{{ \Carbon\Carbon::createFromFormat('YmdHis.uZ',$aplq_result['whenchanged'][0]) }}</td>

Maybe it requires some different approach, I have been looking and it seems like some people are talking about helpers and service injection for calling functions from views, maybe that would resolve this hopefully.

Please or to participate in this conversation.