Search code examples
phphtmlstrtotime

PHP date string from html week input


I have a php file that has a dropdown of 6 selections. The first 4 options are a preset 4 hours ago, 8 hours ago, 16 hours ago, 24 hours ago option. The other two are "select range" and "select date". Select range creates 2 new inputs which are html date inputs. The select week, the one I am asking about, creates a html week input. Once the user selects all inputs they hit submit on the form. The dropdown is a select field inside of a form field. This refreshes the page (php file).

The first thing the php does is get what the inputs are from the updated url and stores them in globals. The first 5 options are easy to get the dates from, but week values are "year-W#". This week is "2020-W26" for example. Is there a strtotime that easily converts this to a date string in php?

<form action="" method ="get">
    <label for="timePeriod">Utilization Hours:</label>
    <div id="timeInputs" style="display: inline">
        <select name="timePeriod"  id="timePeriod" onchange="showDateInputs(value)">
            <option value="4">past 4 Hours</option>
            <option value="8">past 8 Hours</option>
            <option value="16">past 16 Hours</option>
            <option selected value="24" >past 24 Hours</option>
            <option value="1">select range</option>
            <option value="2">select week</option>
        </select>
    
    </div>
    <input type="submit" name="submit" value="Submit">

  <script type="text/javascript">
      document.getElementById('timePeriod').value = "<?php echo $_GET['timePeriod'];?>";
      
      var newDiv = document.createElement('div');
      newDiv.setAttribute("id", "newDiv");
      newDiv.setAttribute("style", "display: inline;");
      if (document.getElementById('timePeriod').value == 1){
            //alert("no way");
            newDiv.innerHTML = "Start Date <input type='date' name='start' id='start' value='<?php echo $_GET['start']; ?>' min='2018-01-02' max='<?php echo date('Y-m-d'); ?>'> End Date <input type='date' name='end' id='end' value='<?php echo $_GET['end']; ?>' min='2018-01-03' max='<?php echo date('Y-m-d'); ?>'>";
            document.getElementById('timeInputs').appendChild(newDiv);
      }
      else if (document.getElementById('timePeriod').value == 2){
            //alert("yes way");
            newDiv.innerHTML = "Select Week <input type='week' name='start' id='start' value='<?php echo $_GET['start']; ?>' ";
            document.getElementById('timeInputs').appendChild(newDiv);
      }
  </script> 
</form>

Above in the file I have this php:

if( isset($_GET['submit']) )
{
    //$stDate = htmlentities($_GET['startDate']);
    //$enDate = htmlentities($_GET['endDate']);
    $timePer = htmlentities($_GET['timePeriod']);

Can I throw $timePer into an easy strtotime("whatever goes here") is my question.


Solution

  • I think this is what you are looking for:

    echo date("Y-m-d", strtotime("2020W26"));
    

    use date documentation to format the date to however you need it.