Home > Software design >  Bootstrap-weekpicker plugin post date format in form
Bootstrap-weekpicker plugin post date format in form

Time:10-10

I am new to coding so sorry for my probebly easy question.

I am using the plugin Week-Picker-Bootstrap-4. I allows me to open a callender and choose a week. The bootstrap-weekpicker.min.js creates a html input form with the following output after selecting the date Week 41, 2021. This is ok for me but I need this value to be send to my server using post. But now it will post the Week 41, 2021. Is there a way to keep the value Week 41, 2021 present for the user to see but to post the original selected date in this format format :'yyyy-mm-dd' as value to my server? I can not find a answer on how to do this.

              <div class="row">            
    <div class="col-xs-12 col-md-12">Week / Year</div>
    <div class="col-xs-12 col-md-12">
<div class="col-auto">
<div class="input-group date align-items-center">
                   
          <div class="input-group date align-items-center">
                 <div id="planning_weekpick"></div>
   </div></div>
    </div></div>
   
      </div>
//Bootstrap css and jquery are inlcuded to.
<link rel="stylesheet" href="https://cdn.rawgit.com/pingcheng/bootstrap4-datetimepicker/master/build/css/bootstrap-datetimepicker.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js" type="text/javascript"></script>
      <script src="https://cdn.rawgit.com/pingcheng/bootstrap4-datetimepicker/master/build/js/bootstrap-datetimepicker.min.js" type="text/javascript" ></script>
      <script src="../js/bootstrap-weekpicker.min.js" type="text/javascript" ></script>
          <script type="text/javascript">
          $(function () {

  $('#planning_weekpick').weekpicker();

});

       
    </script>     

CodePudding user response:

I think you can it with this solution. It is setISODate function.

$date = new DateTime();
$date->setISODate($year, $weekNumber);
echo $date->format('Y.m.d');

There are several methods to extract year and week numbers from Week 41, 2021 string.

For example:

$dateString = 'Week 41, 2021';
$parts = explode(' ', $dateString);
$weekNumber = (int) rtrim($parts[1], ',');
$year = (int) $parts[2];

CodePudding user response:

Thanks to the code and document suggestion of @shanginn. I was able to create the folwoing code to got what i needed ! Many thanks !

$dateString = $planning_week;
$parts = explode(' ', $dateString);
$weekNumber = (int) rtrim($parts[1], ',');
$year = (int) $parts[2];
$weekyear = $weekNumber. '-' . $year;

$date = new DateTime();
$date->setISODate($year, $weekNumber);
$createddate = $date->format('Y-m-d');
$dateplanning = new DateTime($createddate);
$monthnumber = $dateplanning->format("m");
$yearandmonth = $year . '-' . $monthnumber;

function normalizeMonth($monthnumber) {
    switch($monthnumber) {
        case "01": return "Januari";
        case "02": return "Februari";
        case "03": return "Maart";
        case "04": return "April";
        case "05": return "Mei";
        case "06": return "Juni";
        case "07": return "Juli";
        case "08": return "Augustus";
        case "09": return "September";
        case "10": return "Oktober";
        case "11": return "November";
        case "12": return "December";
        
    }
    
    return $monthnumber;
}
$month = normalizeMonth($monthnumber);
  • Related