sayedsohail Posted October 8, 2007 Share Posted October 8, 2007 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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 8, 2007 Share Posted October 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted October 8, 2007 Author Share Posted October 8, 2007 thanks, i am trying to do this on client side, if you noticed its generating the inspection date from serverside and when the user selects the frequency1(no of months) from 1..24, it just needs to calculate and show the expiration date. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2007 Share Posted October 8, 2007 in that case it belongs in the javascript forum. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.