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

FounderStartup's avatar

Call to a member function addDays() on string ?

I need to add the number of days in a date field. What will be the correct code ?

$user->subscriptionexpiry is of type date.

  $user = User::find(Auth::user()->id);

            if ($user->subscriptionexpiry > today())
                // then add purchased days to existing expiry date
                $subscriptionexpiry = ($user->subscriptionexpiry)->addDays($Package->days);
            else
                $subscriptionexpiry = Carbon::now()->addDays($Package->days);

            User::where('id',$user->id)->update(['subscriptionexpiry' =>  $subscriptionexpiry ]);
0 likes
4 replies
jdc1898's avatar

is the date an instance of a Carbon date?

This works: $date = $date->addDays($daysToAdd);

If it isn’t, you can always pass it into Carbon to do the calculations.

1 like
FounderStartup's avatar

@jdc1898 Thanks for your time. I tried to pass it into Carbon but got an error. Can you correct the code ?

            $subscriptionexpiry = Carbon::($user->subscriptionexpiry)->addDays($Package->days);
jdc1898's avatar
jdc1898
Best Answer
Level 21

Try this:

use Carbon\Carbon;

$date = Carbon::parse($user->subscriptionexpiry); $newDate = $date->addDays($Package->days);

1 like

Please or to participate in this conversation.