Jump to content

Date Function: Calculating Days...


complex05

Recommended Posts

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 intervals

Now 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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 days
FROM visitorparking_slotdata
WHERE MONTH(Finish) = '8' AND MONTH(Start) = '8'
GROUP BY Username
HAVING 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
Link to comment
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.