• charlyanderson

    (@charlyanderson)


    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

    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;
    }
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I can help you, but you say:
    “So last period date -> current date (displayed in weeks)”

    Huh? How is that how long the user has been pregnant? Do you not want the time to now from conception? I take it that is somewhere between $firstdaytime and $lastdaytime. So if we assume conception is 2 days after $firstdaytime, and we can get the current timestamp with time(), then the number you are looking for is calculated with:
    $weeks_pregnant = round(( time() - $firstdaytime - 2 ) / ( 7 * 24 * 3600 ), 2);

    If I’ve misinterpreted your intent, my apologies. To get exactly what you asked, substitute $lasttime for the $firstdaytime - 2

    You’ve done a nice job for being new to PHP. There may be better ways to do a few things, but that is a matter of opinion. If neither of the options presented turn out to be what you need, I suspect you can figure out how to adapt, but if not, let me know what you really need and I’ll adjust the code.

Viewing 1 replies (of 1 total)
  • The topic ‘PHP calculation for plugin’ is closed to new replies.