Julian Posted June 12, 2007 Share Posted June 12, 2007 Hi guys. I have this table id_hotel | ratesin | ratesout | rate 1 | 2007-06-07 | 2007-07-15 | 100.00 1 | 2007-09-07 | 2007-12-10 | 150.00 2 | 2007-05-07 | 2007-08-10 | 150.00 3 | 2007-04-07 | 2007-09-10 | 120.00 What I want to do is to make a selection and show only records between desired dates, example: If I post via form: id_hotel = 1 ratesin=2007-07-07 and ratesout=2007-07-10, the result will show only the records in those dates range in this case the result will be: id_hotel | ratesin | ratesout | rate 1 | 2007-06-07 | 2007-07-15 | 100.00 Maybe is easier than I think, but I know someone know how to achieve this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/55337-pick-records-between-dates/ Share on other sites More sharing options...
pocobueno1388 Posted June 12, 2007 Share Posted June 12, 2007 Try this: <?php mysql_query("SELECT * FROM table WHERE ratesin > $ratesin AND ratesout < $ratesout") or die (mysql_error()); ?> $ratesin and $ratesout would of course be the post data variables. Quote Link to comment https://forums.phpfreaks.com/topic/55337-pick-records-between-dates/#findComment-273520 Share on other sites More sharing options...
Julian Posted June 12, 2007 Author Share Posted June 12, 2007 Thanks for the help but I tried this and did not work. I think the reason is if I put >= or >= the query checks the within the column dates. I need to check between those two dates columns. I hope I explained well. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/55337-pick-records-between-dates/#findComment-273526 Share on other sites More sharing options...
obsidian Posted June 13, 2007 Share Posted June 13, 2007 The way you're asking it, you simply need to be sure that your submitted start and end dates are between the start and end of the rate chart dates. However, this will cause a problem when the dates submitted overlap two different rates in your table. What you will probably need to do is loop through the date range entered and query the price for each date. This way, you can get a different rate for each day without causing an error: <?php $start = "2007-07-10"; $end = "2007-08-04"; $ts = strtotime($start); $rates = array(); for ($i = strtotime($start); $i <= strtotime($end); $i += (60 * 60 * 24)) { $date = date('Y-m-d', $i); $sql = mysql_query("SELECT rate FROM rate_table WHERE '$date' BETWEEN ratesin AND ratesout"); if (mysql_num_rows($sql) == 1) { // Found a rate for this date $rates[] = array($date, mysql_result($sql, 0, 'rate'); } } echo "<pre>\n"; print_r($rates); echo "</pre>\n"; ?> This should give you a multi-dimensional array with the dates and rates of all the individual dates of your stay. You can then simply tally the rates and get the grand total. Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/55337-pick-records-between-dates/#findComment-273575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.