Jump to content

Need help accurately calculating a rental return date based on todays date


sigkill-9

Recommended Posts

First, i'm a beginner so please bare with my ignorance. I need some help calculating a return date for between 2 and 8 days from the current day.

 

The details:  I'm creating a movie rental form where the user can select a movie to rent then select a return date (no more than 8 days from the current date) but I need the form to be smart enough to see that if a user selects a rental duration of 8 days starting on March 31st, for example, that it sets the return date for April 7th, not Janurary 39th lol (which is all i've managed to do so far... hence the reason i'm here... plus, it doesnt work rite anyway...  :-\ )

 

Here is the code I have so far that simply explodes the current date, and adds the rental duration (up to 8 days from current day) to the current day, then implodes it back together to output the rental return date to the customer:

 

$today = date("m/d/y");
$duration = $_POST['rentalDuration'];
$explodeDate = explode("/",$today); 
$month = $explodeDate['0'];
$day = $explodeDate['1'] + $_POST['duration'];
$year = $explodeDate['2'];
$returnDateArray = array($month, $day, $year);
$returnDate = implode('/', $returnDateArray);

//The output:
<div>Return Date: <?php echo $returnDate; ?></div>

 

Any help would be greatly appreciated, thanks  8)

Thanks! It works great, but one problem.

 

When I first load the page, it shows the return date as 12/31/69  lol. How would I alter the code you posted to show "the current date + 2 days" as the default rental return date, then have it update as needed when the user selects a different return date?

I played with it and figured out how to display a default return date, then if the user changes the return date it updates as it should, but I'm still curious to see how others would do it. I think my solution may be rather clunky and "noobish" so I'm open to other ideas.

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.