-1

I have a simple web form that has a date field with the HTML 5 date input set:

<input type="date"  id="dateOfBirth" name="dateOfBirth" placeholder="MM/DD/YYYY" required>

Depending on what browser the user uses to complete the form the $_POST['dateOfBirth'] value is either:

05/22/1996

for desktop browsers or:

2020-03-20

for mobile devices etc.

I ultimately need the result in MM/DD/YYYY format for inserting into my database, so the first version is what I'm after here. How can I dynamically detect the format it arrives in and convert to MM/DD/YYYY regardless of the input format?

user982124
  • 4,416
  • 16
  • 65
  • 140
  • What do you mean by "coming through"? Form submission? Date inputs always internally store the date value in 'YYYY-MM-DD', regardless of display format - see [here](https://stackoverflow.com/a/9519493/4205384). You should always be getting the same format. – El_Vanja Mar 19 '20 at 23:06
  • @El_Vanja I mean the form input value after it has been submitted - what I get from $_POST['dateOfBirth'] – user982124 Mar 19 '20 at 23:08
  • That is weird. I tested on my machine and even though the display was `mm/dd/yyyy`, the submitted value was `yyyy-mm-dd`. Are you making any additional modifications to the value? – El_Vanja Mar 19 '20 at 23:10
  • @El_Vanja no not making any modifications to the value, just calling `$_POST['dateOfBirth']` to retrieve it – user982124 Mar 19 '20 at 23:12
  • I believe with input type="date" you will get `yyyy-mm-dd` – user982124 Mar 19 '20 at 23:15
  • try adding form content-type ="application/x-www-form-urlencoded" and accept-charset="UTF-8" – Saud Khan Mar 19 '20 at 23:15
  • Have you maybe tested with a browser that [doesn't support](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date#Browser_compatibility) the type? I tested on IE now and it simply treated it as a regular text input. – El_Vanja Mar 19 '20 at 23:17
  • Yes I have tested on different browsers and get the 2 different types of dates for the `$_POST['dateOfBirth']` after the form has been submitted. Looking for how to detect each type and convert to `mm/dd/yyyy` if it comes through in `yyyy-mm-dd` – user982124 Mar 19 '20 at 23:29
  • 1
    Well, you can always format using `DateTime`. But the problem is - if you get a value from a browser that doesn't support it, the user can enter whatever he damn well pleases. You can't be sure it'll be a `mm/dd/yyyy` date. – El_Vanja Mar 19 '20 at 23:31
  • [The value will *always* be `yyyy-mm-dd`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date). Note that different browsers show different date pickers on the front end, and the format shown in the browser will depend on your locale (and won't match `yyyy-mm-dd`), but the *actual* value, and what is sent to the server, is *always* `yyyy-mm-dd`. Triple check you don't have some JS or PHP modifying that. – Don't Panic Mar 20 '20 at 00:00
  • Where input type date is supported, the placeholder text is ignored. The format will be whatever the browser decides it should be, the **value** will always be YYYY-MM-DD. If the default language is en-US, it will be mm/dd/yyyy and if any other English it will be dd/mm/yyyy. Browsers that don't support type date (e.g. Safari) will allow anything at all. Why you chose the peculiar mm/dd/yyyy format for database storage is beyond me. – RobG Mar 20 '20 at 07:05

1 Answers1

2

DateTime::format date_create() and date_format(), this will reformat the date to your desired format before you insert into DB. Returns the formatted date string on success or FALSE on failure.

if(isset($_POST['dateOfBirth'])){
    $entered = $_POST['dateOfBirth'];
    $create = date_create($entered);
    // If user is using a browser that does not support HTML:5 input type date. 
    // check to make sure the entry is indeed in the proper format that supports date_format() 
    if($create !== false){
         $date = date_format($create, 'm-d-Y');
         // Insert into DB
    }else{
         // Handle failure error reporting
    }        
}

IMPORTANT NOTE: As El_Vanja pointed out there is no way to know what the user is inputting without proper front-end formatting if the user does not have HTML5 and has JS turned off.

You could get the posted date, format and then post it back and have the user confirm their entry is correct.

For example: user enters 03/05/2020 you could then run this through datetime and display as date_format($create, 'F-j-Y'); returning March-5-2020 and have them confirm this entry, then once confirmed, insert into DB as your originally desired format.

Validation could also be done using an explode method.

if(isset($_POST['dateOfBirth'])){
    $entered = $_POST['dateOfBirth'];
    $create = date_create($entered);

    // If user is using a browser that does not support HTML:5 input type date. 
    // check to make sure the entry is indeed in the proper format that supports date_format()    
    if($create !== false){
        $date = date_format($create, 'm-d-Y');
        // explode and create variables for each section of date 
        list($month, $day, $year) = explode("-", $date);
        // now you have each section of the datetime format in its own variable
        // validation can be passed through conditionals for $month and $day
        if($month <= 12){
            $returnDate = date_format($create, 'F-j-Y');

            // display this format to user and confirm
            // echo $returnDate and simple yes/no button form here 
            if(isset($_POST['yes'])){
                // set success css on input:focus display success to user
                // update DB

            }else{
                // set warning css on input:focus display warning to user
                // empty input form and have them reenter date.
            }
        }else{
            // handle $month error
        }
        // repeat conditional for $day
    }else{
        // $create === false --> handle error
    }
}

Another way to do this however would be to validate select fields that have specific entries of values for month. Then you can format on the back end with the returned post variables and be assured the formatting is done correct with regard to day/month.

dale landry
  • 7,831
  • 2
  • 16
  • 28
  • This isn't entirely error-proof. For example, how do you know if `03/04/2020` is March the 4th or April the 3rd? The format is unknown, thus you can't rely on creating a `DateTime` object to make sure user input is right. – El_Vanja Mar 20 '20 at 14:21
  • 1
    @El_Vanja This is true, inherently it would be nearly impossible to tell that without heavy front-end coding to error check the entry of the customer as the two entries for the day and/or month could be up to or equal to twelve. You could however have the user double check the entry by changing format to display as `F/j/Y` and say something like `your entry is March 3rd, 2020?` then have the user confirm. – dale landry Mar 20 '20 at 19:43
  • Confirmation seems like a plausible workaround. Especially if you can format it to the user's locale. – El_Vanja Mar 20 '20 at 20:26