I have this form where users are adding their sessions:

week.png

What I want to do is to show the week number in the input "week" id. My HTML tag for the service date that is on the image is:

<div class="row mt-2">
<div class="col-lg-3 col-sm-6">
<div class="form-floating">
<input id="serviceDate" class="form-control" required name="sessiondate" type="date" onchange="updateInput()"/>
<label for="startDate">Session Date</label>
</div>
</div>

I just searched online and see this PHP code for me to have the week number of the date:

<?php echo date("oW", strtotime("2011-12-31"));?>

And what I want is to get my Service date input id inside that PHP tag "2011-12-31", like I want to write something like calling the id element inside the PHP tag but I'm not sure how to write it or is that even possible?

If you want the week number to dynamically change when the end-user changes the Session Date, then you will want to use Javascript, not PHP, to update the week.

Can you please show the code you currently have for the javascript updateInput() function? What you would want to do is modify that function so that when serviceDate changes, you're also changing what populates in the Week field. That is, if I am understanding you correctly.

commented: I don't have javascript for that one since I can't get how to have the weeknumber date. I just saw that PHP code and that's what I'm trying to do. +0

Sorry, I'm not understanding you. You would want it to be Javascript, not PHP. Something like this:

function updateInput()
{
    var dateInputField = document.getElementById('serviceDate'); // This is the Session Date input field
    var sessionDate = new Date(dateInputField.value); // This is the date entered in the input field
    var firstDayOfYear = new Date(sessionDate.getFullYear(), 0, 1); // This is January 1st, <year of sessionDate>

    // This logic is a little off, I found it via a Google search but there might be something better out there
    var dayOfYear = Math.floor((sessionDate - firstDayOfYear) / (24 * 60 * 60 * 1000));
    var weekOfYear = Math.ceil(( sessionDate.getDay() + 1 + dayOfYear) / 7);

    // Provided that the input box for the week has the ID serviceWeek
    document.getElementById('serviceWeek').value = weekOfYear;
}

Just found this:

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: https://weeknumber.com/how-to/javascript

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
                        - 3 + (week1.getDay() + 6) % 7) / 7);
}

// Returns the four-digit year corresponding to the ISO week of the date.
Date.prototype.getWeekYear = function() {
  var date = new Date(this.getTime());
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  return date.getFullYear();
}
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.