A
A
andrew-kondaurov2021-10-31 16:07:48
PHP
andrew-kondaurov, 2021-10-31 16:07:48

How to add month to timestamps value in database to renew subscription?

In the Laravel project, I have a Subscriptions table that contains the following data:
id ; user_id ; end_date ; status; creation_date
617e94f0ec7f8441339796.png

On the button to renew the subscription, you need to add a month to end_date, withdraw money from the user's account and replace this entry in db.
Here is the function of this button:

public function buysubs(Request $request)
    {
        $amount = 100;
        $user_id = Auth::id();
        $permission = Permission::where('user_id', $user_id)->get()->first();
        $user_balance = Balance::where('user_id', $user_id)->get()->first();
        $Subscriptions = Subscription::where('user_id', $user_id)->get()->first();
        $enddate = Subscription::where('user_id', $user_id)->first('end_date');
      
        if (isset($user_balance->balance) && $user_balance->balance < '100') {
            echo 'нет деняг';
        }

if (!$Subscriptions) //отображается кнопка "купить подписку"
{
    $Subscriptions = new Subscription();
    $Subscriptions->user_id = $user_id;
    $Subscriptions->creation_date = date("Y-m-d H:i:s");
    $Subscriptions->end_date = date("Y-m-d H:i:s", strtotime("+1 month"));
    $Subscriptions->status = '1';
    $Subscriptions->save();
} // покупка подписки вроде работает
else ($Subscriptions->end_date > date("Y-m-d H:i:s"))
{
    //???
    $enddate = //???
    $Subscriptions->status = '1';
    $Subscriptions->save();
    $enddate->save();
}

$user_balance->balance -= $amount;
$transaction = Transaction::create ([
    'user_id' => $user_id,
    'amount' => '-100',
    'description' => 'Подписка на курс'
   ]);
$user_balance->save();

return view('dashboard', ['Subscriptions' => $Subscriptions], ['permission' => $permission]);

}

How to write an else construct to add a month to an already existing record in the database with an overdue date and a date that has not yet come to an end?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-10-31
@andrew-kondaurov

$Subscriptions->end_date =  date("Y-m-d H:i:s",  strtotime($Subscriptions->end_date . " +1 month"));
$subscription->save();

PHP strtotime online

J
jazzus, 2021-10-31
@jazzus

$subscription->end_at = now()->addMonth();
$subscription->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question