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

NoLAstNamE's avatar

calculate percentage on expiry date

Good day everyone, I'm working on showing progress by percentage when to reach the expiry date based on the current date.

I have this code now

// $value->expiry_date = "2021-02-21"

$date_now = Carbon::now();
$date_of_expiry = Carbon::parse($value->expiry_date);
$diff = $date_of_expiry ->diffInDays($date_now);
$wl = 45 - $diff;
$total = 45; // total days of trial
$percent = $wl / $total * 100;
dd($percent);

I have the output below, which is I'm thinking wrong because the date now is the beginning of the trial, it should be 0 and tomorrow it will change, that is what I'm expecting.

2.22222222

also when I try to add 44 days on $date_now it's already 0 on date difference, is it correct or I'm just confused on the Carbon dates.

$date_now = Carbon::now()->addDays(44);
$date_of_expiry = Carbon::parse($value->expiry_date);
$diff = $date_of_expiry ->diffInDays($date_now);
dd($diff)

output 0

Working code snippet for demo password: 12345678

https://laravelplayground.com/#/snippets/fbe6b25b-a6e4-4f45-9ab3-cb51dc744b4b

0 likes
2 replies
a4ashraf's avatar

@benjamin1509

when you are getting a date with now() it actually includes the current day as well but

when you are using the addDays() function it not include the current day that's why it showing the difference

try it like this

$trial_days = 45;

$date_now = Carbon::now()->addDays($trial_days);
$date_of_expiry = Carbon::parse($value->expiry_date);
$diff = $date_of_expiry ->diffInDays($date_now);
$percent = ($trial_days - $diff)/ $total * 100;

dd($percent);

Please or to participate in this conversation.