complex05 Posted August 23, 2006 Share Posted August 23, 2006 Hello,I have a table for visitor parking passes. There are three columns: Start (date) Finish (date) Username (text)Passes can expire in either 1, 2 or 3 day intervalsNow the problem I'm having is a user can only have a maximum of 12 days per month. My current query is this: //$count_reserves = mysql_query("SELECT id FROM visitorparking_slotdata WHERE username='$thisuser' AND DATE_FORMAT(start,'%m')='$month'"); //$count_reserves = mysql_num_rows($count_reserves); if ($count_reserves >= $maxreserves) { echo<<<endhtml You have exceeded the maximum amount of reservations ($maxreserves) by an owner for this month. Please contact management for more information.endhtml; }This doesn't work because it's giving me 12 passes instead of 12 days. I cannot use the MySQL function DateDiff because my MySQL version doesn't support it. I'm sure I have to do it in PHP using mktime but I don't know how to get a 'number of days' value. Any help is much appreciated!Thanks,Barry Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/ Share on other sites More sharing options...
ronverdonk Posted August 23, 2006 Share Posted August 23, 2006 There is a date/time class (Simple Data Time Object) at phpclasses.org that can achieve what you are looking for.Look at [url=http://www.phpclasses.org/browse/package/1952.html]http://www.phpclasses.org/browse/package/1952.html[/url]Ronald 8) Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-79369 Share on other sites More sharing options...
Corona4456 Posted August 23, 2006 Share Posted August 23, 2006 So does the maximum # of reservations include the number of days the passes are for?i.e. a 3 day pass = 3 total reservations? Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-79371 Share on other sites More sharing options...
craygo Posted August 23, 2006 Share Posted August 23, 2006 Can try this[code]$total = 0;$sql = "SELECT * FROM visitorparking_slotdata WHERE username='$thisuser' AND DATE_FORMAT(start,'%m')='$month'"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_array($res)){ $sday = date("z", strtotime($r['start])); $eday = date("z", strtotime($r['finish'])); $diff = $eday - $sday +1; echo "$diff<br>"; $total += $diff; }echo $total;[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-79377 Share on other sites More sharing options...
complex05 Posted August 23, 2006 Author Share Posted August 23, 2006 ah good ol strtotime i didn't even think of that... let me give it a shot, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-79395 Share on other sites More sharing options...
Barand Posted August 23, 2006 Share Posted August 23, 2006 [code]SELECT Username, SUM(TO_DAYS(Finish) - TO_DAYS(start) + 1) as daysFROM visitorparking_slotdataGROUP BY UsernameHAVING SUM(TO_DAYS(Finish) - TO_DAYS(start) + 1) > 12[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-79460 Share on other sites More sharing options...
complex05 Posted August 24, 2006 Author Share Posted August 24, 2006 Thank you, great query! Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-79879 Share on other sites More sharing options...
complex05 Posted August 24, 2006 Author Share Posted August 24, 2006 Now we are having a different problem, how do I modify the query so it will only look for how many reservations are in the current month? Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-80005 Share on other sites More sharing options...
Barand Posted August 24, 2006 Share Posted August 24, 2006 How do you define "in current month"?If it must be entirely in the current month then[code]SELECT Username, SUM(TO_DAYS(Finish) - TO_DAYS(start) + 1) as daysFROM visitorparking_slotdataWHERE MONTH(Finish) = '8' AND MONTH(Start) = '8'GROUP BY UsernameHAVING SUM(TO_DAYS(Finish) - TO_DAYS(start) + 1) > 12[/code]If it started last month and finishes this month, or starts this month and finishes next, then it gets a bit more complex Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-80011 Share on other sites More sharing options...
complex05 Posted August 24, 2006 Author Share Posted August 24, 2006 That's fine, if there are exceptions where the expiry runs into the next month, it's no big dealthanks a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/18453-date-function-calculating-days/#findComment-80017 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.