Hi there.

I need assistance with, I guess, a PHP date function.

There is a way I can figure this, but its really not reliable and I guess it won't even work. So all you guru's, here is what I need to achieve;

I am just writing a simple reservation script in PHP. Now what I need to achieve, is this;

When someone wants to make a reservation, the interface will give the option where the user has to choose how many nights to sleep, and the check-in date, or, the check-in date and check-out date. Now this I can achieve. But here comes the, I guess, tricky part.

Lets say my check-in date is 1 Jan 2008. Check out date is 5 Jan 2008. Now when booking, the application has to reserve all the days, from the starting date, to the end date. With me? Now the other thing that I thought about, is when a user book at the end of a month until in the new month, or for example 28 Dec 2008 until 4 Jan 2009.

So I need to somehow calculate the dates inbetween, and then also it has to be recorded.

Since my English is probably not that good, I'll try to find an example.

Please guys, assist me with this one.

Regards from South Africa.

Member Avatar for langsor

Okay, I had to figure this out just now -- never worked with dates much before -- but it was fun.

This should give you a solid foundation to build the rest of your reservations code on top of.

<?php

// Populate values from form submission ...

// arrive date -- required
$arr_yr = 2008; // = $_POST['arr_yr'];
$arr_mo = 12;   // = $_POST['arr_mo'];
$arr_dy = 28;   // = $_POST['arr_dy'];

// validate arrival date form values here ...

// leave date
$lve_yr = 2009; // = $_POST['lve_yr'];
$lve_mo = 1;    // = $_POST['lve_mo'];
$lve_dy = 4;    // = $_POST['lve_dy'];

// -and/or- by length of stay
$length = 7;    // = $_POST['length'];

// Calculate and record days ...
$alpha = new DateTime( "$arr_yr-$arr_mo-$arr_dy" );
$delta = new DateTime( "$arr_yr-$arr_mo-$arr_dy" );

if ( $length ) {
  $omega = new DateTime( "$arr_yr-$arr_mo-$arr_dy" );
  $omega->modify( "+$length day" );
} else {
  // validate leave date form values here ...
  $omega = new DateTime( "$lve_yr-$lve_mo-$lve_dy" );
}

while ( ( $delta->format('Ymd') + 0 ) <= ( 0 + $omega->format('Ymd') ) ) {

  // record your reserved dates here ...
  // mysql_query( 'INSERT INTO `reservations`... etc ...' );
  
  // see this page for all available date formats
  // http://us3.php.net/manual/en/function.date.php
  
  print $delta->format( 'F d Y' )."<br>\n"; // example code
  $delta->modify( "+1 day" ); // required code
}

?>

Hope it helps

...

Wow thank you.

Havn't tried it yet, but the code seems that it will be working!

I'll let you know tommorrow-its 3am here and I need to get in bed ;-)

Thank you sooooo much.

Member Avatar for langsor

You're very welcome.

Post back here if you get stuck and I'll try and help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.