Jump to content

Calculate date? £50 question


sayedsohail

Recommended Posts

Hi everyone,

 

I got three fields in my form,

 

1. The first is inspection date which is by default current date in dd/mm/yyyy format.

2. Frequency (months) which is a select box values from 1..24.

3. Next due date - which suppose to be inspection date + Frequency(Months) i.e, 1..24

 

I am trying to add number of months to the date supplied in my form and set  nextduedate= inspectiondate +Frequency(Months).

 

Here is my code, which is not giving the results, i.e, if is select or change the frequency to 1 or 24 it just sets nextduedate to 1/10/107.

 

<?php
?>
<html>
<title>calculate next date</title>
<head>
<link rel="stylesheet" href="../style/datepicker.css" type="text/css" />
<script type="text/javascript" language="text/javascript" src="../functions/datepicker.js"></script>

<script type="text/javascript">
function add_months() {
    // date in dd/mm/yyyy
startdate=document.getElementById('idate').value;
//from 1 to 24
no_of_months =document.getElementById('frequency1').value;
  var date  = startdate.substring(0,2);
  var month = startdate.substring(3,5);
  var year  = startdate.substring(6,10);
  startdate = month+"/"+date+"/"+year;

  var startdate = new Date(startdate)
  var curr_mon  = startdate.getMonth()+1;
  var next_mon  = curr_mon+1;

  //dayIncrement=0;
  for (i=0;i<no_of_months;i++) {
    today_date = startdate.getDate();
    curr_mon = startdate.getMonth()+1;
    
    if (curr_mon==next_mon) {
      next_mon = next_mon+1;
    }
    if (startdate.getMonth() == 0 || startdate.getMonth() == 11) {
      no_of_months = no_of_months + 1;
    }
    

  }

  var date = startdate.getDate();
  var month = startdate.getMonth()+1;
  var year = startdate.getYear();
  enddate = date+"/"+month+"/"+year;
document.getElementById('nextdate').value=enddate;

  
}
</script>
</head>

<body>
<table class='aside' width='100%'>
<tr>
<td>Inspection Date:</td>
<td>Frequency:</td>
<td>Next Due Date</td>
</tr>
<tr>
<?php
$today = date('d/m/Y');
?>
<input type="text" name="idate" id="idate" value="<?print $today;?>" onclick="displayDatePicker('idate','','dmy','/');"/>
</td>
<td><select name="frequency1" ID='frequency1' style="width: 150px;font-family: Verdana; font-size: 10pt; color: #0000FF; background-color: #FFFAD0" size="1" onchange='add_months();'>
<?php
for($i=1;$i<=24;$i++) {
echo "<option value=$i>" .$i. "-Month"."</option>";
}
?>
</select></td>
<td><input type='text' name='nextdate' id ='nextdate'</input></td>
</tr>
</table>

</body>
</html>

Link to comment
Share on other sites

Use the strtodate()[/function]

 

Example: (untested)

<?php
$start = '01/10/2007'; // dd/mm/yyyy format
$freq = $_POST['freq'];
list($dd,$mm,$yy) = explode('/',$start)
$end = date('d/m/Y',strtotime($mm . '/' . $dd . '/' . $yy . '+' . $freq . ' months')); // strtotime needs the date in mm/dd/yyyy format
echo $end;
?>[/code

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.