Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:10:10+00:00 2026-05-29T21:10:10+00:00

I am trying to do a Drug Half life calculator with PHP. I want

  • 0

I am trying to do a Drug Half life calculator with PHP. I want to pass in the amount of the drug taken per day in MG’s and pass in the Half-life hours, then it will calculate how much of the drug is left after X amount of time and how much is still left from previous doses.

So far this is what I have…

function calcHalfLife( $mgTaken , $drugHalfLifeHours , $day = 1 ) {
    //total number of half-lifes elapsed
    $total_half_lifes = ($day * 24) / $drugHalfLifeHours;
    //total reduction in dosage
    $reductionFactor = pow( 0.5 , $total_half_lifes );
    //return the current dosage in the person's system
    return round( $mgTaken * $reductionFactor , 8 );
}

Then I am working on this function below which will let me pass in an Array of Days and the MG taken for each day, the function should then iterate the array and run the function above on each day’s value.

function HalfLifeChart(array $days, $drugHalfLifeHours ) {
    $out = array();
    foreach ($days as $day => $dosage) {
        $out[$day] = calcHalfLife( $dosage , $drugHalfLifeHours , 1 );
    }
    return $out;
}

Example usage…

$day = array(1 => 30,
             2 => 0,
             3 => 0,
             4 => 40,
             5 => 30,
             6 => 10,
             7 => 60);

echo '<br><pre>';
print_r(HalfLifeChart( $day, 4.5));
echo '</pre><br><br>';

Now I have a pretty good start but the HalfLifeChart function is where I need to do more work, right now it will run the Half-life calculations on the number passed for each day which is good, but I need to get the result from the previous day and add that to the MG taken on the current day and then run the Calculations on that number.

So for example, if I have 0.8043mg left from the previous day and I took 30mg today, then the calculation should be ran on 0.8043 + 30 and then pass that result through my Half life calculator function.

I am not sure how to grab the result from the previous day though, any help please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:10:14+00:00Added an answer on May 29, 2026 at 9:10 pm
    function HalfLifeChart(array $days, $drugHalfLifeHours ) {
      $out=array();
      $remains=0;
      foreach ($days as $day => $dosage) {
        $total=$remains+$dosage;
        $out[$day]=$total;
        $remains=calcHalfLife( $total , $drugHalfLifeHours , 1 );
      }
      return $out;
    }
    

    gives you

    print_r(HalfLifeChart( $day, 4.5));
    Array
    (
        [1] => 30
        [2] => 0.74409424
        [3] => 0.01845587
        [4] => 40.00045776
        [5] => 30.99213701
        [6] => 10.76870236
        [7] => 60.26709765
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
In Drug.as I have a button, the listener is in Main and I'm trying
Trying to convert a perl script to php. In perl I have a hash
Possible Duplicate: How to properly escape a string via PHP and mysql I'm trying
Trying to send a PHP email the easy way but I cannot work out
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how I can do this properly. The print_r looks like
Trying to copy the msdn refernce here doesn't work and gives an error I
Trying to get a wildcard search to pick up on any text in org_name
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.