PHP Calculate Weeks between 2 dates

Hi there.

I am trying to do the following:

  1. User inputs a date into a form and submits the form
  2. On submission of the form, a PHP Script will calculate how many weeks have passed since that date and redirect them to a page specific to the total.

For example:

I input 12/01/2011
I click Submit
PHP Script calculates that 1 week has passed since that date and redirects me to page1.html

I input 05/01/2011
I click Submit
PHP Script calculates that 2 weeks have passed since that date and redirects me to page2.html

There are 40 pages in total, so the script can redirect me to up to 40 different pages dependent on the number of weeks passed or another page for anything over 40.

This might be complicated… it might be easy. I’ve no idea. I did find 2 tutorials, one for calculating the number of weeks between two dates and 1 for redirecting to a page determined by an if statement. I tried to mash them together but it’s not working.

Can somebody please help me out?

This is what I have so far… but I think it’s probably way off:

<?php

$lmpdate = $_POST['lmpinput'];; // the LMP Date chosen by the user.

$currentDate = mktime();     // Today's date.

$dateSplit = explode("-", $date);

//$dateSplit[0] = Month
//$dateSplit[1] = Day
//$dateSplit[2] = Year

$previousDate = mktime(0, 0, 0, $dateSplit[0], $dateSplit[1], $dateSplit[2]);


$nrSeconds = $currentDate - $previousDate; // substract the previousDate from the currentDate to see how many seconds have passed between these two dates

[B]$nrSeconds = abs($nrSeconds); // in some cases, because of a user input error, the second date which should be smaller then the current one
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; // will give a negative number of seconds. So we use abs() to get the absolute value of nrSeconds
[/B]

$nrWeeksPassed = floor($nrSeconds / 604800);


}

if($nrWeeksPassed = "1") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup1.html\\">";
}

elseif($nrWeeksPassed = "2") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup2.html\\">";
}

elseif($nrWeeksPassed = "3") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup3.html\\">";
}

elseif($nrWeeksPassed = "4") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup4.html\\">";
}

elseif($nrWeeksPassed = "5") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup5.html\\">";
}

elseif($nrWeeksPassed = "6") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup6.html\\">";
}

elseif($nrWeeksPassed = "7") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup7.html\\">";
}

elseif($nrWeeksPassed = "8") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup8.html\\">";
}

elseif($nrWeeksPassed = "9") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup9.html\\">";
}

elseif($nrWeeksPassed = "10") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup10.html\\">";
}

elseif($nrWeeksPassed = "11") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup11.html\\">";
}

elseif($nrWeeksPassed = "12") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup12.html\\">";
}

elseif($nrWeeksPassed = "13") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup13.html\\">";
}

elseif($nrWeeksPassed = "14") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup14.html\\">";
}

elseif($nrWeeksPassed = "15") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup15.html\\">";
}

elseif($nrWeeksPassed = "16") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup16.html\\">";
}

elseif($nrWeeksPassed = "17") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup17.html\\">";
}

elseif($nrWeeksPassed = "18") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup18.html\\">";
}

elseif($nrWeeksPassed = "19") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup19.html\\">";
}

elseif($nrWeeksPassed = "20") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup20.html\\">";
}

elseif($nrWeeksPassed = "21") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup21.html\\">";
}

elseif($nrWeeksPassed = "22") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup22.html\\">";
}

elseif($nrWeeksPassed = "23") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup23.html\\">";
}

elseif($nrWeeksPassed = "24") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup24.html\\">";
}

elseif($nrWeeksPassed = "25") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup25.html\\">";
}

elseif($nrWeeksPassed = "26") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup26.html\\">";
}

elseif($nrWeeksPassed = "27") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup27.html\\">";
}

elseif($nrWeeksPassed = "28") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup28.html\\">";
}

elseif($nrWeeksPassed = "29") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup29.html\\">";
}

elseif($nrWeeksPassed = "30") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup30.html\\">";
}

elseif($nrWeeksPassed = "31") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup31.html\\">";
}

elseif($nrWeeksPassed = "32") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup32.html\\">";
}

elseif($nrWeeksPassed = "33") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup33.html\\">";
}

elseif($nrWeeksPassed = "34") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup34.html\\">";
}

elseif($nrWeeksPassed = "35") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup35.html\\">";
}

elseif($nrWeeksPassed = "36") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup36.html\\">";
}

elseif($nrWeeksPassed = "37") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup37.html\\">";
}

elseif($nrWeeksPassed = "38") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup38.html\\">";
}

elseif($nrWeeksPassed = "39") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup39.html\\">";
}

elseif($nrWeeksPassed = "40") {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup40.html\\">";
}

else {
 echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\">";
}

endif;

?>

I have also highlighted part of the code above in BOLD because this part I need to change… instead of using “abs” to turn a negative figure into a positive I want an “if” statement that will produce an error if it is a minus figure telling them they need to input a date prior to today’s date.

Lastly, I’m not quite sure how to setup the HTML form to pass the date to the “lmpinput” field in the PHP Script. I need a form where the user will select the Day and Month from drop downs and type the full year (YYYY) into a text field.

I know this sounds like a lot to ask but this is really important so if there is a PHP expert out there who knows how to fix my script… I’d love to hear from you!

<?php
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

$currentDate = mktime();     // Today's date.
$previousDate = mktime(0, 0, 0, $day, $month, $year);

$nrSeconds = $currentDate - $previousDate; // substract the previousDate from the currentDate to see how many seconds have passed between these two dates

if($nrSeconds < 0)
{
	// Do something with the error here.
}
else
{
	$nrWeeksPassed = floor($nrSeconds / 604800);
	
	if($nrWeeksPassed < 40)
	{
		echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup$nrWeeksPassed.html\\">";
	}
	else
	{
		echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\">";
	}
}
?>

I’ve taken the liberty to tidy and optimise your code a little.

Instead of having ~40 if statements, I’ve reduced that down to 1 if/else that will check if the weeks are < 40, and redirect as appropriate.

We’ve added the day, month, year fields to support the dropdowns and text box you’re after.

Now, your form will just need the 3 fields, named day month and year. That should be easy enough for you to sort out, and decide what you want the error to do/say when it returns a negative.

Thank you so much Rob - I will try this out when I get home :slight_smile:

Can I be a real pain and ask another question…

If I wanted to change it up a bit and reverse the calculation, how would I go about it. What I mean is…

If a user was to put a “future” date in, how would I get the form to

a) calculate how many weeks are left UNTIL that date.
b) substract that number from 40
c) redirect them to the pagenumber relevant to that total.

The end result should send them to the same page as this script, it’s just a different way of reaching the same total.

The reason I ask is that I’m trying to write a subscription script for 40 diffrent newsletters depending on how many weeks pregnant somebody is. The script we’ve written here determines this by asking them when their last menstrual cycle started, which should give us an estimate of how many weeks pregnant they are.

Some women don’t know the answer to this question, but they do know their due date. So I want to take the due date, see how many weeks they have to go and subtract that from 40 (the average number of weeks in a pregnancy) which will then leave me with an estimate on how long they have been pregnant (number of weeks)…

Oh and one more question… Your “if” statement above… where it checks if the total is under 40, does that mean anything under “and including” 40?

Is this doable? Am I even making sense?

Or… I’ve just thought of an easier way to do it…

If they don’t know their LMP date.

Take the due date,. subtract 40 weeks from that to give us the LMP date and then use this date to put us back to the beginning of the script we’ve already designed above?

Would that be easier?

Essentially you’d take the same function, and just reverse it.

Well spotted Watson! You’ll want to change the < 40 to < 41 or <= 40 to include the 40th week.

Anything’s doable with PHP! :smiley:

6 of 1 really. Either way is going to be about as much code as the other.

Hi Rob,

I’ve come up with this… but it’s not working.

What have I got wrong?

<?php
$lmpday = $_POST['lmpday'];  //Taken from the "Day" dropdown of the LMP Date
$lmpmonth = $_POST['lmpmonth'];  //Taken from the "Month" dropdown of the LMP Date
$lmpyear = $_POST['lmpyear'];  //Taken from the "Year" field of the LMP Date

$dueday = $_POST['dueday'];  //Taken from the "Day" dropdown of the Due Date
$duemonth = $_POST['duemonth'];  //Taken from the "Month" dropdown of the Due Date
$dueyear = $_POST['dueyear'];  //Taken from the "Year" field of the Due Date
 
$currentDate = mktime();     // Today's date in UNIX Time
$previousDate = mktime(0, 0, 0, $lmpday, $lmpmonth, $lmpyear); //The LMP Start Date in UNIX Time
$duedate = mktime(0, 0, 0, $dueday, $duemonth, $dueyear);  //The Due Date in UNIX Time
$lmpcalc = $duedate - 24192000; // The Due Date minus 40 weeks (Calculated LMP Date) in UNIX Time
 
$nrSeconds = $currentDate - $previousDate - $lmpcalc;  //This subtracts the LMP Date & the Calculated LMP Date (one of which should be zero if the above statement has worked it's magic) away from Today's Date and returns the number of seconds in between.

if($nrSeconds < 0)  //This checks the form has been filled in, if not it tells the user to enter their details.
{
echo "<span style='error'> We need some information from you before we can register you for our newseltter. Please fill in either your Due Date or the start date of your Last Menstrual Cycle.</span>";
}
else
{
    $nrWeeksPassed = floor($nrSeconds / 604800);  //This turns the number of seconds passed into weeks.

    if($nrWeeksPassed < 41)  //This checks the number of weeks is under 40
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup$nrWeeksPassed.html\\">";  //This redirects the user to the relevant signup form for the correct number of weeks.
    }
    else  //Meaning "for anything over 40 weeks"
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\">";  //This redirects the user to a page to say their term has already exceeded 40 weeks.
    }
}


?>

<br /><br />Please enter your Due Date.
<form action=<?php echo $_SERVER["PHP_SELF"]; ?> method="post">
Date: <select name="dueday">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
Month: <select name="duemonth">
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
Year: <input type="text" name="dueyear" style= "width:60px"/> <i>Format: YYYY</i>
<input type="submit" value="Submit" />
</form>

<br /><br />Or the date your Last Menstrual Cycle started.
<form action=<?php echo $_SERVER["PHP_SELF"]; ?> method="post">
Date: <select name="lmpday">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
Month: <select name="lmpmonth">
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
Year: <input type="text" name="lmpyear" style= "width:60px"/> <i>Format: YYYY</i>
<input type="submit" value="Submit" />
</form>

I’ve commented it quite heavily whilst it’s in development so please forgive the mess… I will remove the comments prior to launch :slight_smile:

I click submit and it just refreshes the form. It doesn’t redirect.

Please ignore my last post… I was too late to edit it so I’m sorry for double posting…

I fixed that error!

I’ve taken out everything to do with the “Due Date” for now and I’m only working with the LMP Date. I’ve also placed the HTML Form on a seperate page instead of making it post to itself because after I removed the due date references it was running before I had a chance to enter any data.

So this is what I have now for the php script:

<?php
$lmpday = $_POST['lmpday'];  //Taken from the "Day" dropdown of the LMP Date
$lmpmonth = $_POST['lmpmonth'];  //Taken from the "Month" dropdown of the LMP Date
$lmpyear = $_POST['lmpyear'];  //Taken from the "Year" field of the LMP Date

//$dueday = $_POST['dueday'];  //Taken from the "Day" dropdown of the Due Date
//$duemonth = $_POST['duemonth'];  //Taken from the "Month" dropdown of the Due Date
//$dueyear = $_POST['dueyear'];  //Taken from the "Year" field of the Due Date

$currentDate = mktime();     // Today's date in UNIX Time
$previousDate = mktime(0, 0, 0, $lmpday, $lmpmonth, $lmpyear); //The LMP Start Date in UNIX Time
//$duedate = mktime(0, 0, 0, $dueday, $duemonth, $dueyear);  //The Due Date in UNIX Time
//$lmpcalc = $duedate - mktime(24192000); // The Due Date minus 40 weeks (Calculated LMP Date) in UNIX Time

$nrSeconds = $currentDate - $previousDate; //- $lmpcalc;  //This subtracts the LMP Date & the Calculated LMP Date (one of which should be zero if the above statement has worked it's magic) away from Today's Date and returns the number of seconds in between.

if($nrSeconds < 0)  //This checks the form has been filled in, if not it tells the user to enter their details.
{
echo "<span style='error'> We need some information from you before we can register you for our newseltter. Please fill in either your Due Date or the start date of your Last Menstrual Cycle.</span>";
}
else
{
    $nrWeeksPassed = floor($nrSeconds / 604800);  //This turns the number of seconds passed into weeks.

    if($nrWeeksPassed < 41)  //This checks the number of weeks is under 40
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup$nrWeeksPassed.html\\">";  //This redirects the user to the relevant signup form for the correct number of weeks.
    }
    else  //Meaning "for anything over 40 weeks"
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\">";  //This redirects the user to a page to say their term has already exceeded 40 weeks.
    }
}


?>

And this is what I have for the HTML form, on a seperate page:

<form action="pregsignup.php" method="post">
Date: <select name="lmpday">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
Month: <select name="lmpmonth">
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
Year: <input type="text" name="lmpyear" style= "width:60px"/> <i>Format: YYYY</i>
<input type="submit" value="Submit" />
</form>

The script is only working if the date is enter is 01,01,2011 - If I enter that date, it correctly passes me to the signup page for week2.

If I enter any other date… let’s say I go back a week it gives me the error message defined in our “if($nrSeconds < 0)” statement.

Oooh I fixed it…

I just changed

$previousDate = mktime(0, 0, 0, $day, $month, $year);

to

$previousDate = mktime(0, 0, 0, $month, $day, $year);

I keep forgetting I have to pretend I live in America…

Now on to the due date calculation!!!

You’re going to hate me by the time you come to read this reply.

Please ignore everything I’ve written since you last replied… Here is the most up to date script… and you’ll be glad to know IT WORKS!!!

So just in case anyone who reads this is trying to figure out how to do something similar…

This is the script I am using to calculate how many weeks pregnant a user is when they enter their due date:

<?php

$day = $_POST['day']; 
$month = $_POST['month'];
$year = $_POST['year']; 
 
$currentDate = mktime();
$duedate = mktime(0, 0, 0, $month, $day, $year);
 
$nrSeconds = $currentDate - $duedate;  

$nrSeconds = abs($nrSeconds); 

if($nrSeconds < 0)  
{
echo "<span style='error'> We need some information from you before we can register you for our newseltter. Please fill in either your Due Date or the start date of your Last Menstrual Cycle.</span>";
}
else
{
    $nrWeeksPassed = floor($nrSeconds / 604800); 

    if($nrWeeksPassed < 41)  
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup$nrWeeksPassed.html\\" >"; 
    }
    else  
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\" >"; 
    }
}


?>

And this is the PHP Script I am using to calculate how many weeks a user is pregnant if they provide their LMP Date.

<?php
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
 
$currentDate = mktime();     
$previousDate = mktime(0, 0, 0, $month, $day, $year); 
 
$nrSeconds = $currentDate - $previousDate; 
 
if($nrSeconds < 0)
{
echo "<span style='error'> We need some information from you before we can register you for our newseltter. Please fill in either your Due Date or the start date of your Last Menstrual Cycle.</span>";
}
else
{
    $nrWeeksPassed = floor($nrSeconds / 604800); 
    
    if($nrWeeksPassed <= 40)
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup$nrWeeksPassed.html\\">";
    }
    else
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\">";
    }
}
?>

Then I have two HTML Forms…

1 to collect the due date and 1 to collect the LMP date… I won’t post them here because the forms are pretty self explanitory and afterall, this is the PHP forum…

There is just 1 thing I have to figure out:

These two lines here (in both scripts)

        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregsignup$nrWeeksPassed.html\\">";
    }
    else
    {
        echo "<meta http-equiv=\\"refresh\\" content=\\"0;URL=pregnonews.html\\">";

currently “redirect” me to the correct page… however, they reload in the same page. I want them to load into an iframe named “signup_form”

You might better understand what I mean if I draw you a layout of my page (see attached iframes.jpg)

At the moment, the script runs in it’s own page, exactly as designed. How do I amend the above two pieces of code so that it produces the desired result in the right hand iframe?

I’m sorry to be a pain with this… As I’m sure you have figured out by now, PHP isn’t my strong point. I’m a designer more than I am a programmer and I’ve been tasked with developing this as well as designing it.

For what it’s worth… as soon as I can get this last piece of the puzzle. The script is complete :slight_smile:

That’s the most important thing! :slight_smile:

I’d suggest using an ajax request to load the blocks, rather than iframes which are rather outdated nowadays. My javascript skills aren’t too flash hot though, so I’d suggest another post in the javascript forum may yield better results.

I much prefer back-end coding - you don’t have to make it look pretty!