Jump to content

Recommended Posts

Hate working with dates in MySQL, but here's the problem:

 

"SELECT * FROM reservations
WHERE (datefrom BETWEEN '{$year}/{$month}/01'
AND '{$year}/{$month}/{$daysInMonth}')";

 

That works almost perfectly, except for it won't return reservations that overlap months (in the next month anyway). So if the datefrom date is january 29th, it won't show the reservation in february... The dates are stored in "date" (YYYY-MM-DD) format.

 

I sort of come close to working it out then my mind goes blank and i can't think... doing my head in! Anybody have any possibilities?

 

Cheers, Adam!!

Link to comment
https://forums.phpfreaks.com/topic/125137-selecting-dates/
Share on other sites

Reckon I've got the logic right, but can't get it to work properly...

 

<?php

$resSQL =  "SELECT * FROM reservations
		WHERE (datefrom BETWEEN '{$year}/{$month}/01'
		AND '{$year}/{$month}/{$daysInMonth}'
		OR ((datefrom BETWEEN '{$year}/{$month}/01'
		AND '{$year}/{$month}/{$daysInMonth}')
		AND (dateto BETWEEN '{$year}/{$nextMonth}/01'
		AND '{$year}/{$nextMonth}/{$daysInMonth}')))";

?>

 

What is working is that it's returning any reservations where the starting date is in $month .. however still the overlapping reservations are left out, both any at the start or end of month..

 

I may have overused brackets starting at the "OR"... All variables are correct, double checked. I think it's just something wrong with either bracket use or slight logic problem. What does a fresh pair of eyes see?

 

Any help appreciated, Cheers!

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/125137-selecting-dates/#findComment-647194
Share on other sites

By the look of your query it seems php provides the values for the month and the year?!

I may be wrong but the query looks good to me, it can be the script?

 

Please post more of the script? Maybe this can clarify some things.

 

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html

 

8 out 10 problems on my scripts are because of MySQL queries :-[  so i`m going on a whim here . .

Link to comment
https://forums.phpfreaks.com/topic/125137-selecting-dates/#findComment-649461
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.