Level 21
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
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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 ]);
Try this:
use Carbon\Carbon;
$date = Carbon::parse($user->subscriptionexpiry); $newDate = $date->addDays($Package->days);
Please or to participate in this conversation.