How does Laravel compare the time gap between the 'created_at' and 'updated_at' fields?

    3 answers 3

    As far as I know, you can use the time () function to calculate the difference, and then determine the multiplicity of what you need. If you count not hunting can be applied.

    print date("H:i:s", mktime(0, 0, difftime())); $diffdate =date("H:i:s", mktime(0, 0, difftime())); 

    And then pass it on:

     return view('pages.blog')->with([ 'diffdate' => $diffdate ]); 

      Laravel uses Carbon for its dates, this object has a diff method that displays the difference between dates as a DateInterval object, or there are more detailed methods diffInDays , diffInHours , diffInMinutes , etc.

      It looks like this:

       $user = User::find(1); $diff = $user->updated_at->diffInMinutes($user->created_at); 

      The output will be the number of minutes between dates, if you want your format, use the diff method.

      • "Laravel uses Carbon for its dates" - Incorrect. Without additional settings, it returns dates in MySQL format. - Mrak
      • @Mrak why? I said - for my dates, my dates are created_at , updated_at , mutators are immediately connected to them by default - Yaroslav Molchan
      • Now it is clear. - Mrak
      • Maybe I have Laravel old (5.3), but now I checked it - and the usual string in the Mysql format is returned - Mrak
      • @Mrak from version 4 was like this, it’s possible that the model reassigned dates or turned off altogether, put a clean Laravel out of the box and see it, now it’s got into the dock itself and there’s a line like this - By default, Eloquent will convert instances of Carbon, which I said - Yaroslav Molchan
       echo floor((strtotime($x->updated_at) - strtotime($x->created_at)) / 60);