如何使用PHP解释HTML5输入日期值

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. Would you be able to help me with this?

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

  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.

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 browser is older, then browser will make it a normal TEXT input and STILL you will not see any difference. Cheers, Boyan

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

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

Demo

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.