Compare date with Carbon in Laravel

2

I want to implement a logic in my laravel backend using the date of a publication.

The example is as follows:

A user creates an article, this contains title, content, image, user owner and date of publication, it turns out that the user published this article but had one or several spelling errors and wishes to edit it, but this was noticed hours later I would like to avoid this, I would like to compare the date in minutes with Laravel using Carbon or some native php function by having it say if it compares if the date of the article was greater than 30 minutes can not be edited. this:

if($article->create_at > 30min){
  return abort(403, 'No tienes permisos para editar ya este artículo');
}
    
asked by vdjkelly 21.07.2017 в 23:26
source

1 answer

2

With Carbon there are several ways to do it, one of them is with diffInMinutes() , taking as reference the date of creation of the article:

if ($article->created_at->diffInMinutes() > 30) {
    return abort(403, 'No tienes permisos para editar ya este articulo');
}

More information and ideas in Carbon's documentation: link

    
answered by 22.07.2017 / 03:09
source