If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2

Thread: PHP calculation

  1. #1
    Join Date
    Sep 2012
    Member #
    3 times

    PHP calculation

    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;}
    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;} 


  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Cottage Grove, Minnesota
    Member #
    723 times
    Funny ... you're like me and belong to other forums as well.

    I answered this in another forum ... so I'll just post the same answer here (for others who might be interested):

    I used this formula right out of Wikipedia ...
    Naegele's rule - Wikipedia, the free encyclopedia

    // 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.
    Datepicker | jQuery UI


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 05:24 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: