Hello I am trying to add a 7, 14 or 30 day to our job posting online form. Right know I just have a start and end date drop down. What I have in mind is to do 3 radio buttons with 7 day, 14 day, and 30 day, the I will assing the start day but I want the end date to be calculated automatically deppending on which radio Button was chosen. Is this possible?


Thank you

Dani AI

Generated

A short, practical add-on to the thread: the cleanest UX is to calculate the end date immediately in the browser when a duration radio is chosen and the start date is picked, and then re-check the same calculation on the server before saving. That gives instant feedback (no round-trip) while keeping the database safe from tampered form values. and have already shown server-side/date ideas and reports the approach works — the snippet below gives a simple client-side implementation that fits a form with a start field (HTML5 type="date" or three selects) and radio buttons for the duration.

// auto-fill "end" from a start field and radios named "duration"
(function(){
  function parseFromDateInput(val){
    if(!val) return null;
    if(val.indexOf('-')>-1){
      var p = val.split('-');
      return new Date(Number(p[0]), Number(p[1]) - 1, Number(p[2]));
    }
    var d = new Date(val);
    return isNaN(d.getTime()) ? null : d;
  }

  function getStartDate(){
    var start = document.getElementById('start');
    if(start && start.value) return parseFromDateInput(start.value);
    var m = document.getElementById('s_month'),
        d = document.getElementById('s_day'),
        y = document.getElementById('s_year');
    if(m && d && y && m.value && d.value && y.value)
      return new Date(Number(y.value), Number(m.value)-1, Number(d.value));
    return null;
  }

  function formatISO(dt){
    var mm = (dt.getMonth()+1).toString().padStart(2,'0');
    var dd = dt.getDate().toString().padStart(2,'0');
    return dt.getFullYear()+'-'+mm+'-'+dd;
  }

  function updateEnd(){
    var sel = document.querySelector('input[name="duration"]:checked');
    var start = getStartDate();
    if(!sel || !start) return;
    var days = parseInt(sel.value,10);
    var end = new Date(start.getTime());
    end.setDate(end.getDate() + days);
    var endEl = document.getElementById('end');
    if(endEl) endEl.value = formatISO(end);
  }

  document.addEventListener('change', updateEnd);
  document.addEventListener('DOMContentLoaded', updateEnd);
})();

Server-side notes (no code block here): always recompute the end date on the server from the posted start and the posted duration (cast duration to integer), reject impossible values, and store dates in a consistent date-only format. If an off-by-one appears, check timezone handling and prefer building dates from year/month/day parts rather than relying on string parsing.

Recommended Answers

All 5 Replies

<?php //add_days.php
if(isset($_REQUEST['submit'])){
	$start_date="2007-11-25";
	$add_days=$_REQUEST['days'];
	$new_date = date('Y-m-d', strtotime($start_date.' + '.$add_days. ' days'));
	echo $new_date;
}
?>
<html>
<body>
<FORM METHOD=POST ACTION="add_days.php">
<INPUT TYPE="radio" NAME="days" value="7">7<br />
<INPUT TYPE="radio" NAME="days" value="14">14<br />
<INPUT TYPE="radio" NAME="days" value="30">30<br />
<INPUT TYPE="submit" name="submit" value="submit">
</FORM>
</body>
</html>

This will do the work for you.

Cheers.
Nav

This code work the only thing I see a problem is this

$start_date="2007-11-25";

IS there any way to detect the actual day automatically?

Sorry I am new to PHP still learning

Thank you

Joaquiin encinas

The actual day or date ?

Current date :

$today = date('Y-m-d');
echo $today; // This will print for example 2007-11-30

Current day :

$today = date('d');
echo $today; // This will print for example 30

For more options or format -> http://bg2.php.net/date

OK this looks like it will work could you look at this

jobs.yumasun.com/AESgenericII/new_job.php

In the bottom of the formo you will see drop downs for start and end date. above that I will have the radio buttons then after a radio button is selected I will select the start date in the drop down an acording to that a calculation will be made and utomatically wwill set the end date

Ok this work guys very well
I had this to calculate the start date and write it to the db

$s_month = ($_REQUEST);
$s_day = ($_REQUEST);
$s_year = ($_REQUEST);
$start = $s_month . '/' . $s_day . '/' . $s_year;

I used the php scrip that nav33n send and modify it to this

$today = date('m/d/Y');

$add_days=$_REQUEST;

$end = date('m/d/Y', strtotime($start.' + '.$add_days. ' days'));

and it works The start auto selected to actual date but also can be modify and the end date is calculated based on the radio button selection and start date. Very cool

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.