javascript - calculate and display difference between 2 dates before submitting form -


i find find way calculate difference between 2 dates, , display result before submit form. similar check in - check out drop downs in yellow/ orange box in http://www.booking.com/ on selecting dates number of nights displayed.

html version of code displayed in js fiddle: http://jsfiddle.net/chri_chri/xy2gpa02/2/

code used generate drop down date selectors

  <?php    // displays days leading 0s   $options = array();     ($i=1; $i<32; $i++) {    $theday = date('d', mktime(0,0,0,0,$i,2000));    $sel = ($i == date('d') ? 'selected="selected"' : '');    $options[] = "<option value= \"{$theday}\" {$sel}>{$theday}    </option>";   }    $options_list = join("\r\n", $options);   echo "<div class='select' id='date'><select class=\"short-input\" name=\"day_from\">{$options_list}</select></div>";       ?>    <?php   $options = array();    ($i = 1; $i<13; $i++) {   $themonth = date('f', mktime(0,0,0,$i,2, 2000));   $month = date('m', mktime(0,0,0,$i,2,2000));   $sel =($i == date('n') ? ' selected="selected"' : '');   $options[] = "<option value=\"{$month}\" {$sel}>{$themonth}   </option>";   }   $options_list = join("\r\n", $options);   echo "<div class='select' id='month'><select class=\"short-input\" name=\"month_from\" size=\"1\">{$options_list}</select></div>";   ?>    <?php    /* build selection list year */   $today = time(); // stores today's date   $startyr = date("y", $today); // year $today  echo "<div class='select' id='year'>                             <select class='short-input' name='year_from'>\n";  ($year=$startyr;$year<=$startyr+10;$year++)  {     echo " <option value= $year";     if ($startyr == $year)     {     echo "";  }  echo " > $year</option>\n";  }  echo "</select>\n</div>\n";  ?> 

you have 2 options, use java-script or submit form php file , process form. jsfiddle working example http://jsfiddle.net/mdamia/3b5fyzjl/32/

// script

function staycost() {   var dayfrom = $('.day-from').val();   var monthfrom = $('.month-from').val();   var yearfrom = $('.year-from').val();   var datefrom = date.parse(monthfrom + ' ' + dayfrom + ' ' + yearfrom);   var dayto = $('.day-to').val();   var monthto = $('.month-to').val();   var yearto = $('.year-to').val();   var dateto = date.parse(monthto + ' ' + dayto + ' ' + yearto);   var datedifference = dateto - datefrom;   var diffindays = math.ceil(datedifference / (1000 * 3600 * 24));   $('.days').text(diffindays);   $('.cost').text(diffindays * 40); } staycost(); $('.day-to').change(function () {   staycost(); }); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -