I wrote a little AJAX gadget which sent the string typed to a PHP backend which parsed it using strtotime and then formatting it out again as something unamiguous (like 2 January 2009). Then every time the date entry field is changed by the user (with an onKeyUp event), this AJAX call is triggered and displays the unambiguous form next to the input box, so users can see how their entry is being interpreted. Of course, there's some overhead in the AJAX calls, and it requires JavaScript.

If you wanted to do without JavaScript you could do a similar parse-format sequence when the form is submitted and show a confirmation page with your server's interpretation of the date.


I took this idea and wrote an ajax so that on keyup or blur, the entry is sent to PHP which puts it into strtotime() and if a valid result comes out, formats it something like "Wed January 23, 2008 6:23pm". If the result is invalid, it outputs an "Invalid" message. This is then displayed in a span next to the textbox.

So as the user types they can see if what they entered is valid, and also see how the date/time will be interpreted. Works beautifully, and makes the form very usable.

For what it's worth, here is the code:
The PHP ajax (called judgedatetime.php:
if ($a < 1200103200){
} else {
$b=date("D M j, Y g:ia",$a);
echo $b;

The Javascript
function assessdtime(dTime,putHere){

And HTML embedded in the page..
<input type="text" size="14" id="start" name="start" onkeyup="assessdtime(this.value,'startreport');" onblur="assessdtime(this.value,'startreport');" /> <span id="startreport">Enter a date and time</span>

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to