0

I'm new on PHP and everything in general, I'm building an application where a date picker is shown. I want to submit the date following a specific format (year : "y" and month : "m") to end up with a url like http://localhost/index.php?m=06&y=2019...

the datepicker i'm using is https://getdatepicker.com/5-4/

and the code:

HTML:

                        <div class="col-xl-4 col-md-6 col-12">                         
                            <div class="float-left">
                                <form action="">
                                    <div class="input-group">                                        
                                        <input name="datepicker" id="datepicker" class="form-control py-2 border-right-0 borderdate-pickerwidth:auto" value="04/01/2021" />
                                        <button class="input-group-append">
                                            <i class="mdi mdi-calendar align-middle font-c-x-large border-left-0 border px-2"></i>
                                        </button>
                                    </div>
                                    </form>
                            </div>
                        </div>

JS:

$(function () {
    $('#datepicker').datetimepicker({
        format: 'DD/MM/YYYY'
    });
});

Actually if I submit a form the value is:

?datepicker=04%2F01%2F2021 

but I want:

?m=01&y=2021

So my question is: How can I submit the information displayed on the datepicker and format it as commented above?

Thank you in advice.

  • I don't think you can do it with any of the picker options. Taking a single input and creating several variables from it requires manual work. May I ask why would you want this? If you need it purely for processing, then you can easily extract individual values (year, month, day) in the code that processes the submit. – El_Vanja Jan 27 '21 at 11:50
  • Hello, firstly thank you for your answer, I would like to process some values from the page content depending on the month/year selected (let's say for example, monthly visitors). I found a solution which consist of 2 different inputs with different values, but I would like to make it more simple... How could I extract the individual values? maybe with php explode? – Ferran Casals Jan 28 '21 at 09:25
  • It's best to use the [`DateTime` class](https://www.php.net/manual/en/class.datetime.php). First you create the object through `createFromFormat` and then you can use `format` to extract any individual piece of information like month, year or whatever you need. – El_Vanja Jan 28 '21 at 10:45
  • Cool! Thank you for your answer. I'm also new on stackoverflow, so.. should I upvote this comment? – Ferran Casals Feb 18 '21 at 09:27
  • No action is required on your part here. You only need to consider voting and accepting if the question gets answered. Which I don't feel is necessary here if you've solved your problem by using a suggestion from the comment. – El_Vanja Feb 18 '21 at 09:47

0 Answers0