-1

I have a problem. I've a date input on my site. When the input type date is not supported by the browser, I'm initializing a jQuery datepicker.

The problem is, that the normal date input returns the value like this:

2019-08-23

And the datepicker this way:

26.08.2019

Instead adding a hidden field in the HTML, I want to re-format the received value via PHP to the first format above. So is there a way to build a small function that checks if the format is the datepicker format and which returns the formatted value like 2019-08-26?

I'm receiving the value with $_POST['date'].

Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • which datepicker are you using? – Gabriel Souto Aug 19 '19 at 22:29
  • jQuery datepicker. I know that there is a solution done in the browser with a hidden input which contains the right value but in my case this is much more work than doing a re-formit in PHP. – Mr. Jo Aug 19 '19 at 22:30
  • 1
    Check the datepicker documentation, they usually provide a way to change the format, for instance this is for [jQueryUI](https://api.jqueryui.com/datepicker/#option-dateFormat) – msg Aug 19 '19 at 22:34
  • Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – showdev Aug 20 '19 at 00:31
  • `'2019.08.23'.split('.').reverse().join('-')`. – RobG Aug 20 '19 at 02:57

1 Answers1

1

Something like this should work:

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

Reference: PHP strtotime()

GTodorov
  • 1,993
  • 21
  • 24