5

I need to let the users pick a date (preferable in the format dd/mm/yy) and I decide to try out new HTML5 input date type. However I don't know how to interpret the value it gives on the server side. The value I get is yyyy-mm-dd. How can I do this?

What if the user uses an older browser that does not support it, how will they enter the date?

4 Answers 4

7
  1. Just convert the date using PHP's date functionality (date(), DateTime, etc)

    $datetime = new DateTime('2012-12-06');

    $new_date = $datetime->format('d/m/Y');

  2. They will see a plain text field. It will be up to you to make sure they enter a validate date.

Sign up to request clarification or add additional context in comments.

1 Comment

pls add this to your answer $date = date('d/m/Y', strtotime($_POST['date']));
4

You can convert the date formats using date(), like this,

 $new_date = date('d/m/y', strtotime($_POST['date']));

Demo

Comments

1

If the user has a browser that does not support it, the browser will just display a regular text input box, into which the user could enter anything they like.

You'd therefore need to be able to cope with any arbitrary data being input. But that would apply anyway, since you should never trust user input coming from the browser.

If you want all users to have a nice date control, you could use a jQuery datepicker or similar to backfill the feature for browsers that don't have it.

As for what to do with the yyyy-mm-dd value when you get it -- just use the DateTime() class to turn it into a PHP DateTime object. Easy:

$dateObj = new DateTime($_GET['mydate']);  //or whatever your input field is called.

Comments

0

You can just retrieve it as a normal input. Then you can convert it seamlessly to a date with date(). This whole is only a browser enhancement, and nothing changes to all server-side. If the browser is older, then browser will make it a normal TEXT input and STILL you will not see any difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.