Home > front end >  sum availibilty of user in minute
sum availibilty of user in minute

Time:01-11

I want to calcul the total availability of an user: I store availibilty like this :

And I have history of availibilty cause user can change their availibilty so I need to make the sum with this.

signupDate to first UpdateDATE and after updateDate[i] to updateDate[i 1] and at the end updateDate[i n] to now;

and get for each duration in minute :

endHours - startHours

I got json like this : availibility object are per week

  {
  "2022-12-20" (date when user have signup ): [
         {
        dayNumber : 2,
        startHours:08:00,
        endHours :10:00;
        },
        {
        dayNumber : 3,
        startHours:11:00,
        endHours :16:00;
        }
    ],
        "2022-12-28" (date when user have update his availibilties): [
         {
        dayNumber : 2,
        startHours:08:00,
        endHours :10:00;
        },
        {
        dayNumber : 3,
        startHours:11:00,
        endHours :16:00;
        }
    ],
       "2023-01-01"  (date when user have update his availibilties): [
        {
        dayNumber : 5,
        startHours:05:00,
        endHours :10:00;
        },
        {
        dayNumber : 7,
        startHours:19:00,
        endHours :22:00;
        }
    ]
    
    }

Whats I have start for the moment :

I have count number of weeks beetwen signupDate and now :

$number_of_week = Carbon::parse($user->signupDate)->diffInWeeks(Carbon::now());

Finaly I want to get total availibilty of the user

Thanks

CodePudding user response:

After our chat, I think I got the full picture of the problem, so lets see if we can solve this together:

I'm not sure where the json comes from, so let's just assign it to a variable for now:

$jsonString = '
{
  "2022 - 12 - 20": [
    {
      "dayNumber": 2,
      "startHours": "08:00",
      "endHours": "10:00"
    },
    {
      "dayNumber": 3,
      "startHours": "11:00",
      "endHours": "16:00"
    }
  ],
  "2022 - 12 - 28": [
    {
      "dayNumber": 2,
      "startHours": "08:00",
      "endHours": "10:00"
    },
    {
      "dayNumber": 3,
      "startHours": "11:00",
      "endHours": "16:00"
    }
  ],
  "2023 - 01 - 01": [
    {
      "dayNumber": 5,
      "startHours": "05:00",
      "endHours": "10:00"
    },
    {
      "dayNumber": 7,
      "startHours": "19:00",
      "endHours": "22:00"
    }
  ]
}
';

Then, we can run the code like so:

//I'm assuming that the json only contains availabilities for 1 user
//This creates an associated array out of your json
$assocArray = json_decode($jsonString, true);
$totalAvailabilityInMinutes = 0;
foreach($assocArray as $updatedAvailabilityAt => $availabilityArray) {
    $availabilityOfDayInMinutes = 0;
    foreach($availabilityArray as $availability) {
        $explodedStart = explode(':', $availability['startHours']);
        $explodedEnd = explode(':', $availability['endHours']);
        //Perform calculation from my comment
        $availabilityOfDayInMinutes = ($explodedEnd[0] * 60   $explodedEnd[1]) - ($explodedStart[0] * 60   $explodedStart[1]);
        dump("Availability of day {$availability['dayNumber']}: $availabilityOfDayInMinutes");
        $totalAvailabilityInMinutes  = $availabilityOfDayInMinutes;
    }
}
dump($totalAvailabilityInMinutes);

Please note that I used 2 variables to store the minutes, one per day, and one accumulating the days. You can pick which one is most applicable to you.

I've tested the code locally, it should work and provide correct numbers ;)

Edit:

So since you already have an array, not a json, you could skip this step:

$assocArray = json_decode($jsonString, true);
  • Related