I added the code for generating the due dates for present day.

Can you Please help me how can i generate the vaccination due date by selecting the date of birth.

Here is my code in

app/design/frontend/ultimo/default/template/vaccination/vaccination.phtml

 <?php
    $nextWeek = time() + (7 * 24 * 60 * 60);
               // 7 days; 24 hours; 60 mins; 60 secs
    echo 'Now:       '. date('d-m-Y') ."\n";
    //echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
    // or using strtotime():
    echo 'Three Week: '. date('d-m-Y', strtotime('+3 week')) ."\n";
    echo 'Six Week: '. date('d-m-y', strtotime('+6 week')) ."\n";
   ?>

This is the date of birth in

app/design/frontend/ultimo/default/template/child/child.phtml

 <form action="" method="post" id="form-validate"> <li> <label class="required"for="birthday"><em>*</em><?php echo $this->__('Date Of Birth')?></label> <div class="input-box dob"> <select name="month" class="input-text required-entry validate-select" style="width: 100px;"onchange="call()" > <option value="">Month</option> <option value="1">January</option> <option value="2">Febuary</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <select name="day" class="form-error" style="width: 100px;" id="day"> <option value="">Day</option> </select> <select name="year" class="form-error" style="width: 100px;" id="year" > <option value="">Year</option> </select> </div> </li>

***<div class="button"> <p class="required"><?php echo $this->__('* Required Fields') ?></p> <button id= "dueDateCalSubmit" type="Submit" class="button"    title="<?php echo $this->__('Generate') ?>" name="send" id="send2"><span><span><?php echo $this->__('Generate') ?></span></span></button> </div>***

<script type="text/javascript"> 

 function call()
{ 
var kcyear = document.getElementsByName("year")[0], 
kcmonth = document.getElementsByName("month")[0], 
kcday = document.getElementsByName("day")[0]; 
var d = new Date(); 
var n = d.getFullYear(); 
for (var i = n; i >= 2000; i--) 
{ 
    var opt = new Option(); 
    opt.value = opt.text = i;
    kcyear.add(opt);
} 



 kcmonth.addEventListener("change", validate_date); 

function validate_date() 
{
 var y = +kcyear.value, 
 m = kcmonth.value,
 d = kcday.value; 
 if (m === "2") 
    var mlength = 28 + (!(y & 3) && ((y % 100) !== 0 || !(y & 15))); 
 else 
     var mlength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][m - 1];
    kcday.length = 0; 
 for (var i = 1; i <= mlength; i++) 
    { 
        var opt = new Option(); 
        opt.value = opt.text = i; 
        if (i == d) opt.selected = true; 
        kcday.add(opt); 
    } 
} 
validate_date();
} 
 </script> 

Dani AI

Generated

A simple, robust approach is: accept a single, validated date-of-birth value (preferably an ISO "YYYY-MM-DD" string from an HTML5 date input or a hidden ISO field you build from your dropdowns), convert that to a UTC-only Date object, then add the required number of days (weeks * 7) to produce each vaccination due date. This avoids leap/DST surprises and keeps the calculation identical in every browser. was right to push a datepicker for input convenience and server-side DateTime validation — that stays good advice.

// read DOB either from a <input type="date" id="dob"> or from the existing selects
function getDOB() {
  var dInput = document.getElementById('dob');
  if (dInput && dInput.value) {
    var p = dInput.value.split('-'); // yyyy-mm-dd
    return new Date(Date.UTC(+p[0], +p[1]-1, +p[2]));
  }
  var y = +document.getElementsByName('year')[0].value;
  var m = +document.getElementsByName('month')[0].value;
  var d = +document.getElementsByName('day')[0].value;
  return new Date(Date.UTC(y, m-1, d));
}

function addWeeksUTC(date, weeks) {
  var out = new Date(date.getTime());
  out.setUTCDate(out.getUTCDate() + weeks * 7);
  return out;
}

function fmt(date) {
  return String(date.getUTCDate()).padStart(2,'0') + '-' +
         String(date.getUTCMonth()+1).padStart(2,'0') + '-' +
         date.getUTCFullYear();
}

// usage
var dob = getDOB();
console.log('1w:', fmt(addWeeksUTC(dob,1)), '3w:', fmt(addWeeksUTC(dob,3)), '6w:', fmt(addWeeksUTC(dob,6)));

Notes and pitfalls: always re-check on the server (calculate again with PHP DateTime to prevent client manipulation), store dates in ISO format in the DB, and prefer input[type="date"] with a simple JS fallback that assembles ISO from the selects. If a vaccine schedule uses months rather than weeks, add months with UTC-safe month arithmetic instead of multiplying weeks. Since your current form builds day/month/year selects, the getDOB() function above plugs straight into that without changing the UI.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

I haven't considered this code closely, but why aren't you using a datepicker?

So your vet is open 7 days a week?

Don't add seconds in order to change the date, use the DateTime object and add with a DateInterval.

What's going on with all that javascript? A datepicker will give you a calendar.

can you please add datepicker in my code.I tried but it has not worked.

Member Avatar for Member #120589

A datepicker should give you a valid date, whereas separate dropdowns need to be passed to a validator as you can end up with something like 2014-09-31, which is obviously invalid or the issue with leap years.

So the choice is between validation convenience and user convenience (datepickers may be more awkward than dropdowns - especially for mobile users).

Your PHP code though could be modernized:

$dates = [];
$dt = new DateTime(); //now
$dates['one_week'] = $dt->add(new DateInterval('P1W'))->format('Y-m-d');
$dates['three_week'] = $dt->add(new DateInterval('P2W'))->format('Y-m-d');
$dates['six_week'] = $dt->add(new DateInterval('P3W'))->format('Y-m-d');

echo "<pre>";
print_r($dates);
echo "</pre>";

or even:

$dt = new DateTime(); //now
$dates = [$dt->add(new DateInterval('P1W'))->format('Y-m-d'), $dt->add(new DateInterval('P2W'))->format('Y-m-d'), $dt->add(new DateInterval('P3W'))->format('Y-m-d')];

echo "<pre>";
print_r($dates);
echo "</pre>";

Coming back to the datepicker - you can choose a bog-standard HTML5 datepicker (may not display correctly across all browseers - check first) or select a script of your choice, e.g. jQueryUI. There are literally hundreds of these out there.

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.