Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2012
    Thanked 1 Time in 1 Post

    PHP calculation help

    Good evening,

    I am very new to PHP and wondered if someone could help me add something onto a PHP calculation?

    If someone could help it would be a HUGE help :-)

    Basically I am working on a due date calculator... Currently it tells you when you due date will be from last period date. I need it to also say how many weeks pregnant the person currently is.

    So last period date -> current date (displayed in weeks).

    This is the PHP

    PHP Code:
    function ovpredct2_datechooser($name,$value=""

    $value)) $value=date("Y-m-d"); 
    $chooser.="<select name=".$name."month>"
    $chooser.="<option $selected value=$i>$months[$i]</option>"
    $chooser.="</select> / "
    $chooser.="<select name=".$name."day>"
    $chooser.="<option $selected>$i</option>"
    $chooser.="</select> / "
    $chooser.="<select name=".$name."year>"
    $chooser.="<option $selected>$i</option>"
    $chooser.="</select> ";     


    //construct the calculator page     
    $ovcalc="<style type=\"text/css\"> 

    //last cycle date 
    //convert to time 
    //first fertile day 
    $firstdaytime=$lasttime $_POST[days]*24*3600 16*24*3600
    $firstday=date("F d, Y",$firstdaytime); 
    //last fertile day 
    $lastdaytime=$lasttime $_POST[days]*24*3600 12*24*3600
    $lastday=date("F d, Y",$lastdaytime); 
    //have to adjust due date? 
    $diff=$_POST[days] - 28
    //due date $date + 280 days 
    $duedatetime=$lasttime 280*24*3600 $diff*24*3600
    $duedate=date("F d, Y",$duedatetime); 
    //the result is here 
    $ovcalc.='<div class="ovpredct2_table"> 
            Your estimated due date is <strong>'
            <p align="center"><input type="button" value="Calculate again!" onclick="javascript:history.back();"></p> 

    $ovcalc.='<div class="ovpredct2_table"> 
            <form method="post"> 
            When was the first day of your last period?<br /><br /> 
            Usual number of days in your cycle: <select name="days">'

    $ovcalc.="<option $selected value='$i'>$i</option>"
            <p align="center"><input type="submit" name="calculator_ok" value="Calculate"></p> 



  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Cottage Grove, Minnesota
    Thanked 1,169 Times in 1,160 Posts
    I used this formula right out of Wikipedia ...

    // Naegele's rule for calculating pregnancy
    // Example:
    // LMP = 8 May 2009
    //    +1 year = 8 May 2010
    //    -3 months = 8 February 2010
    //    +7 days = 15 February 2010 
    // the first day of the woman's last menstrual period
    $duedate = strtotime(date("Y-m-d", strtotime($date)) . "+1 year" . "-3 months" . "+7 days");
    echo date("F j, Y", $duedate);
    echo "<br />";
    echo "You are ".(40-(floor(($duedate - time()) / 604800)))." weeks along.";

    The output on my browser: February 15, 2010

    The weeks is a bit harder because we'll assume 40 weeks total, less the weeks between today and the calculated due date. You'll have to test several dates to see what the accuracy is on weeks. Just upload the script above to your website and test some different dates to see how close they come to your own calculations.

    I see you ask them this question as well, "Usual number of days in your cycle".
    Parikh's formula is a calculation method that considers cycle duration

    That adds more accuracy as described in the Wikipedia article. You would alter the formula within this line:
    $duedate = strtotime(date("Y-m-d", strtotime($date)) . "+1 year" . "-3 months" . "+7 days");

    A bit of tweaking and you have it with about 3-4 lines of code (not including your JQuery date picker).

    This is the date picker that I recommend you use.

    Last edited by mlseim; 04-16-2014 at 08:43 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts