D
D
Danila5342020-12-07 18:51:39
Laravel
Danila534, 2020-12-07 18:51:39

Carbon, how to add another to one date derived from the database?

Good evening,
the whole point is in the question ...

Wandered through the documentation, and did not manage to find what I need.

$date = Carbon::now()->add(30, 'day');
Adds 30 days to the current date.

But I need to add 30 days to the date from the database, for example.

Is it possible?

For example with the condition:

$information = DB::table('role_user')->where('user_id', Auth::user())->get()->first();

       if(is_null($information->times)) 
{
  $date = Carbon::now()->add(30, 'day');
}
else {
  $date = <b>ВРЕМЯ ИЗ БАЗЫ</b>->add(30, 'day');
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
N, 2020-12-07
@Fernus

1. Working with Carbon ;
2. If you create a model and specify for this field that it is a date ( details ), then you can do it according to your scheme, as you wanted ...

J
jazzus, 2020-12-07
@jazzus

if without a model (in the continuation of the shit code)
Carbon::parse($information->times)->addDays(30);

S
Sergey delphinpro, 2020-12-07
@delphinpro

We make a normal model.
In the model, we cast the field with the date to the date
And we get the finished Carbon field, with which we do what we want.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question