Jump to content

Help determining valid values in a booking form


jacomus

Recommended Posts

I am in the process of developing a webpage for bookings:

 

What I have so far is a page which determines booking dates. There are drop down menus on the HTML page which determine the month and the day of the start of the booking period, and a secondary set of drop down menus which determine the end of the booking period. The PHP page then displays a confirmation of the booking. Currently it is fairly basic as I just began development on the pages.

 

What I need to know is how to send out an error if the booking period exceeds a certain time frame, such as 10 days.

 

I.E. from March 15th until March 24th would be a valid date as it would fall within a 10 day period, but March 15th until March 26th would return an error. What I need to know is how to do this? I am fairly new to PHP, but have been learning the basics fairly quickly, I have been unable to locate a tutorial on these sorts of things. I would be very appreciative of any help that can be given.

The online PHP manual if full of fun.  Here is a link to the date functions...

 

http://us2.php.net/manual/en/ref.datetime.php

 

I would approach this by

1) convert each date strings to epoc (unix timestamp)

2) subtract one from the other and divide by 86400 (seconds in a day)

3) if > 10 error

 

Assumption:  The date string you are using can be parsed by strtotime().  See http://us2.php.net/manual/en/function.strtotime.php

$startEpoc = strtotime($yourStartDate);
$endEpoc = strtotime($yourEndDate);
$daySpan = ($endEpoc - $startEpoc)/86400;
if ($daySpan > 10) {
  //deal with the error
}

 

 

Thanks, I will make an attempt at that. It seems a little more advanced than stuff I have done in PHP so far. What my initial plan to do was to assign values to each month based on how long each month is, I got stuck early on here. When subtracting the start of the booking period from the end booking date, I simply just got the value of the end booking date.

 

IE (for the purpose of experimentation).

 

$booking = $HTTP_POST_VARS['day'];

$booking2 = $HTTP_POST_VARS['day2'];

$total = $booking2 - $booking;

 

Print($total);

 

 

 

For example, on my form page, if I entered the value of 14 for booking and the value of 23 for booking2, the value I would get for $total on the php page would be 23 instead of the expected 9.

Oh! I see.... Lets take a step back...

 

You need to look at the whole date, not just the day.  If you were only dealing for days within a month your approach would be fine but what about Mar 29 - Apr 2.  That is within your 10 day limit but would yield some strange results in your check.  That being said let's add a step before the validation and create a date string.

 

The idea behind the approach I'm highlighting is to turn a date into a simple integer.  That allows you to do simple math to find your date range.

 

Assumptions:

  • You are acquiring month, month2, year and year2 values in the same way you are getting day and
  • Your month is a character (Mar, March, etc) value

 

<?php
$startEpoc = strtotime("{$HTTP_POST_VARS['day']} {$HTTP_POST_VARS['month']} {$HTTP_POST_VARS['year']}");
$endEpoc = strtotime("{$HTTP_POST_VARS['day2']} {$HTTP_POST_VARS['month2']} {$HTTP_POST_VARS['year2']}");
?>

 

Now that you have the dates you can figure out the time between them (in seconds)

 

<?php
$span = $endEpoc - $startEpoc;
// use either floor() to round down or ceil() to round up
$spanInDays = floor($span / 86400);
?>

 

The threshold you mentioned was 10 days so we can do a simple comparison with our now integer span

 

<?php
if ($spanInDays > 10) {
  echo "Too many days\n";
  exit;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.