Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    121
    Thanks
    15
    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=""

        
    $months=array('','January','February','March','April','May','June','July','August'
        
    'September','October','November','December'); 
         
        if(empty(
    $value)) $value=date("Y-m-d"); 
         
        
    $parts=explode("-",$value); 
         
        
    $day=$parts[2]+0
        
    $month=$parts[1]+0
        
    $year=$parts[0]; 
         
        
    $chooser=""
         
        
    $chooser.="<select name=".$name."month>"
        for(
    $i=1;$i<=12;$i++) 
        { 
            if(
    $i==$month$selected='selected'
            else 
    $selected=''
            
    $chooser.="<option $selected value=$i>$months[$i]</option>"
        } 
        
    $chooser.="</select> / "
         
        
    $chooser.="<select name=".$name."day>"
        for(
    $i=1;$i<=31;$i++) 
        { 
            if(
    $i==$day$selected='selected'
            else 
    $selected=''
            
    $chooser.="<option $selected>$i</option>"
        } 
        
    $chooser.="</select> / "
         
        
    $chooser.="<select name=".$name."year>"
        for(
    $i=(date("Y")-1);$i<=2050;$i++) 
        { 
            if(
    $i==$year$selected='selected'
            else 
    $selected=''
            
    $chooser.="<option $selected>$i</option>"
        } 
        
    $chooser.="</select> ";     
         
        return 
    $chooser


    function 
    ovpredct2_generate_html() 

        
    //construct the calculator page     
        
    $ovcalc="<style type=\"text/css\"> 
        .ovpredct2_table 
        { 
            "
    .get_option('ovpredct2_table').
        } 
        </style>\n\n"

         
        if(!empty(
    $_POST['calculator_ok'])) 
        { 
            
    //last cycle date 
            
    $date="$_POST[dateyear]-$_POST[datemonth]-$_POST[dateday]"
             
            
    //convert to time 
            
    $lasttime=mktime(0,0,0,$_POST[datemonth],$_POST[dateday],$_POST[dateyear]); 
             
            
    //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>'
    .$duedate.'</strong>     
            <p align="center"><input type="button" value="Calculate again!" onclick="javascript:history.back();"></p> 
            </div>'

             
        } 
        else 
        { 
            
    $ovcalc.='<div class="ovpredct2_table"> 
            <form method="post"> 
            When was the first day of your last period?<br /><br /> 
            '
    .ovpredct2_datechooser("date",date("Y-m-d")).'<br><br> 
            Usual number of days in your cycle: <select name="days">'

                     
            for(
    $i=20;$i<=45;$i++) 
            { 
                if(
    $i==28$selected='selected'
                else 
    $selected=''
                
    $ovcalc.="<option $selected value='$i'>$i</option>"
            } 
             
            
    $ovcalc.='</select> 
            <p align="center"><input type="submit" name="calculator_ok" value="Calculate"></p> 
            </form>         
            </div>'

        } 

        return 
    $ovcalc


  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I used this formula right out of Wikipedia ...
    http://en.wikipedia.org/wiki/Naegele%27s_rule

    Code:
    <?php
    // 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
    $date="2009-05-08";
    
    $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.
    http://jqueryui.com/datepicker/


    .
    Last edited by mlseim; 04-16-2014 at 07: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
    •