skip to Main Content

I am on laravel mysql and I want a percentage and number of how many new users have been created or left in the past 30 days.
For example, if there are 3 users before 30 days and 2 new users have been created between 30 days so, according to my calculation it should be ((2-3)/3) *100 = -33.33. I don’t know how to get the right percentage. and also if the left users are more then before 30 days users, then it should be shown in red color. I don’t know how to differentiate between positive percentages and negative percentages.

I have atteched the screenshort of what i actully want.
enter image description here

I tried this code but i am not 100% sure about the output.

        $currentDate = Carbon::now();
        $startDate = now()->subDays(30)->startOfDay();


        $startDate = Carbon::now()->subDays(30);
        $totalUsers = User::count();
        $createdUsers = User::where('created_at', '>=', $startDate)->count();

        $number = (($createdUsers - $totalUsers) / $totalUsers+1)  * 100;

2

Answers


  1. To calculate the percentage and number of new users created or left in the past 30 days, you can use the following steps:

    Get the count of users before 30 days:

    $usersBefore30Days = User::whereDate('created_at', '<', now()->subDays(30))->count();
    

    Get the count of new users created in the past 30 days:

    $newUsers = User::whereDate('created_at', '>=', now()->subDays(30))->count();
    

    Calculate the percentage difference:

    $percentageDifference = (($newUsers - $usersBefore30Days) / $usersBefore30Days) * 100;
    

    Determine the color based on the percentage difference:

    $color = $percentageDifference < 0 ? 'red' : 'green';
    

    You can then use the $percentageDifference and $color variables to display the result accordingly in your Laravel view.

    Here’s an example of how you can implement this in a Laravel view:

    <div>
        @if ($percentageDifference < 0)
            <span style="color: red;">{{ $percentageDifference }}%</span>
        @else
            <span style="color: green;">{{ $percentageDifference }}%</span>
        @endif
        <span>{{ $newUsers }} new users</span>
    </div>
    
    Login or Signup to reply.
  2. I’ve tried walking through this with your example numbers, but I do not understand how an increase of users can result in a negative growth value (-33.33%).
    The only ways I can imagine you wanting a negative value are:

    • Either your total number of users has dropped since 30 days ago, in which case your logic needs to allow for this. Counting the total number of users now, and also counting the subset of those created in the past month, is completely blind to any users that might have existed previously, but don’t now.
    • Or you want to compare the number of new users in this period with the number of new users in a previous period, in which case, looking at the total seems entirely irrelevant, and you should be looking at the number of users created between 60 and 30 days ago, and 30 days ago and now.

    Perhaps you do just want to look at the total number of users, relative to 30 days ago. In which case, here’s a walk-through, and suggested code that might achieve that.

    Say you had 3 users before, and an additional 2 users in the past 30 days.
    I assume for that, the absolute number of new users you want is 2, and the percentage you want is 66.67%, because 3 + 2 = 5, and 5 is ~1.67 times your original number, or an increase of ~66.67%.

    $currentDate = Carbon::now();
    $startDate = now()->subDays(30)->startOfDay();
    
    $startDate = Carbon::now()->subDays(30);
    $totalUsers = User::count(); // 5
    $createdUsers = User::where('created_at', '>=', $startDate)->count(); // 2
    
    $number = (($createdUsers - $totalUsers) / $totalUsers+1)  * 100;
    // ((2 - 5) / 5 + 1) * 100
    // ((-3) / 5 + 1) * 100
    // (-3/5 + 1) * 100
    // (2/5) * 100
    // 40
    

    so your code would result in 40, which is seemingly not the desired result.

    Instead, you could do:

    $numberOfUsersNow = User::count(); // 5
    $numberOfNewUsers = User::where('created_at', '>=', $startDate)->count(); // 2
    $numberOfUsersPreviously = $numberOfUsersNow - $numberOfNewUsers; // 3
    
    $percentageIncreaseInNumberOfUsers = $numberOfNewUsers / $numberOfUsersPreviously * 100; // 66.666666666667
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search