问题描述:

How do you make a text field automatically add - after certain numbers.

So if I was typing a date, 20120212, it would automaticly add a - after the first 4 digits, the second two so it outputs like 2012-02-12

网友答案:

You can use PHP's strtotime function combined with date

$old_date = '20120212';
$new_date = date('Y-m-d', strtotime($old_date)); //returns 2012-02-12

And after reading your question was unclear if you needed this done in PHP or JavaScript. But you can also do this with input masking in jQuery. Here is a good plugin: Masked Input Plugin

You would format the input field like:

jQuery(function($){
   $("input").mask("9999-99-99");
});
相关阅读:
Top