X

A blog about digital & online

Adding a customised date field to a booking form

Today I had to help a client add a date field to their booking form, because they’ve been experiencing drop-offs in their enquiries. Adding the date option is meant to help lock in potential leads for a fixed appointment date.

The problem, however, is that they only wanted to make the next 7 days available in the form. So a standard date-picker wouldn’t do.

I used the magic of PHP’s date() function to grab the next 7 days dates and order them into an HTML SELECT input.

Here is the source code I created:

 

 

<?php

// grab todays date
$todays_date = date("Y/m/d");

// set my defaults for later use
$date_count = 0;
$date_arr = array();

// loop forward through the week
while ( $date_count < 7 ) { 

// increment the day by 1 - by using my incrementiing "count" value 
if( $date_count > 0 ) {
 $date_incrementer = strtotime("+{$date_count} day", strtotime($todays_date));
} else {
 $date_incrementer = strtotime($todays_date);
}

// add each incremented day to an array 
array_push($date_arr, $date_incrementer);
 
$date_count += 1;
 
}

// loop a new HTML option into existence, for each day
foreach ($date_arr AS $the_date) {
 echo '<option ';
 if( $quote_date == date("Y/m/d", $the_date) ) { echo 'selected="selected" '; } 
 echo 'value="'.date("Y/m/d", $the_date).'">'.date("j F - l", $the_date).'</option>';
}

?>

The whole script goes inside the opening & closing SELECT elements.

If this script helped you, please share the post or leave us a comment.

About the Author

Phil Smulian
Phil Smulian likes to get his hands dirty when it comes to solving code problems. He also really loves making user experiences better than they were when he found them. Find him on Twitter @Bowsie

Leave a reply

Your email address will not be published. Required fields are marked *