skip to Main Content

I’m building out a form on a hotel for room reservations. Currently, a user can select a check-in and checkout date. When the user selects a check-in or checkout date the format comes through in the URL as yyyy-mm-dd and in the URL as examplesite.com/1234?datein=yyyy-mm-dd. How can I get the date output to be formatted as mm/dd/yyyy?

Currently, this is my input tag:

<input class="date_in" id="datein" placeholder="" value="" type="date" name="datein">

2

Answers


  1. Chosen as BEST ANSWER

    Thank you @Ronak Hapailya and @mmm for your help. I figured out a different workaround because the output couldn't be modified in the format the travel software needed if the field remained a type="date" field.

    This is what I did. I changed the field to a type="text" and made the text field show a datepicker UI.

    The new code for the input fields:

    <input class="date_in" id="DateIn" value="mm/dd/yyyy" type="text" name="DateIn">
    
    <input class="date_out" id="DateOut" value="mm/dd/yyyy" type="text" name="DateOut">
    

    Jquery to make the text field appear as a datepicker field:

    jQuery(function($) {
      $('.date_in').each(function(){
        $(this).datepicker({ dateFormat: 'mm/dd/yy' }).val();
      });
      
      $('.date_out').each(function(){
        $(this).datepicker({ dateFormat: 'mm/dd/yy' }).val();
      });
    });
    

  2. As you cant change the input type date output formate. So instead of this you can change date formate in php

    if (isset($_GET['datein'])) {
        $datein = $_GET['datein'];
        $date = DateTime::createFromFormat('Y-m-d', $datein);
        if ($date) {
            $formattedDate = $date->format('m/d/Y');
            echo 'Formatted Date: ' . $formattedDate;
        } else {
            echo 'Invalid date format.';
        }
    }
    

    like above sample code you can change date formate whatever you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search