24 hours limitation

hii one more
before this what i was using for showing remaining 24 hours in second . but using your query how will i get remaining time

i was using this query but it was not useful in sense that when user posts once it start his time from 24 hours from the same time he post but i want 24 hours time after completing his last 24 hours

$que = mysql_query("SELECT TIMESTAMPDIFF(SECOND,time,NOW()) AS 'Diff' FROM link where userid='$id' order by time desc limit 1");
while($row=mysql_fetch_array($que))



{
$re = $row['Diff'];
$hour = "86400";
$diff = $hour - $re ;

if($diff <= 0)
{
echo "your time has been over";
exit;
}

this is the query which was returning me times in seconds from last post.
But how can i do this using your query
for example whenever user next time post time start i can calculate 24 hours limit for posting ?

Try this:


$sql = "SELECT CONCAT( DATE( NOW() ), ' ', TIME(msn_users.time) ) AS period_end
FROM msn_users
WHERE id = $id";

$result = mysql_query($sql);
$datetime = mysql_result($result, 0, 'period_end');

$period_end = new DateTime($datetime);
$now = new DateTime();

if ($period_end < $now) {
    $period_end->add(new DateInterval('P1D'));
}

$interval = $now->diff($period_end);

echo 'Time until you can post again: ' . $interval->format('H:I:S');

I think it’s easier to do the calculations in PHP in this case. You could do them in the query, but the SQL would probably drive you mad if you look at it for too long :wink:

this is not working its giving out
Time until you can post again: H:I:S

Oops, sorry I made a mistake… it should be:


$interval->format('%H:%I:%S');

hii i am in little confusion
your one query is saying
Time until you can post again: 08:03:09

but your other query saying
You are allow

this is the different output of your both queries ? why

here are the both queries what i am using

  $sql = "SELECT CONCAT( DATE( NOW() ), ' ', TIME(msn_users.time) ) AS period_end 
FROM msn_users 
WHERE id = $id"; 

$result = mysql_query($sql);  
$datetime = mysql_result($result, 0, 'period_end');  

$period_end = new DateTime($datetime); 
$now = new DateTime(); 

if ($period_end < $now) { 
    $period_end->add(new DateInterval('P1D')); 
} 

$interval = $now->diff($period_end); 

echo 'Time until you can post again: ' . $interval->format('%H:%I:%S');  




$que1 = "SELECT count(link.id) as total  
FROM link, msn_users   
WHERE link.userid = msn_users.id   
AND link.dtime > CONCAT( DATE( NOW() ), ' ', TIME(msn_users.time) )   
AND msn_users.id = $id";  

$exe2 = mysql_query($que1);  

if (0 == mysql_result($exe2, 0, 'total')){  
   echo "you are allow";  
} 

I thought that maybe you were planning to do something like this:


$que1 = "SELECT count(link.id) as total
FROM link, msn_users
WHERE link.userid = msn_users.id
AND link.dtime > CONCAT( DATE( NOW() ), ' ', TIME(msn_users.time) )
AND msn_users.id = $id";

$exe2 = mysql_query($que1);

if (0 == mysql_result($exe2, 0, 'total'))
{
    echo "you are allowed to post";
}
else
{
    $sql = "SELECT CONCAT( DATE( NOW() ), ' ', TIME(msn_users.time) ) AS period_end
    FROM msn_users
    WHERE id = $id";

    $result = mysql_query($sql);
    $datetime = mysql_result($result, 0, 'period_end');

    $period_end = new DateTime($datetime);
    $now = new DateTime();

    if ($period_end < $now) {
        $period_end->add(new DateInterval('P1D'));
    }

    $interval = $now->diff($period_end);

    echo 'Sorry, you can only post again in: ' . $interval->format('%H:%I:%S') . ' hours';
}

Brother as you have big knowledge of that all
i just need three condition in my code can you suggest a one simple solution for these all.
first after signup user got 24 hours for posting
if user post in 24 hours then he can’t post until completing last 24 hours. as his 24 hours complete again he got 24 hours for next post and the same procedure continue for every post
and if user can’t post in any 24 hours we can warn him or block him or any condition after completing 24 hours without post

i have remove all old posts data from LINK table
just new enter new post which time is 2013-08-07 15:33:09

and signup time in msn_users table is 2013-08-04 23:05:00

but it saying that you are allow to post
i think i have recently post link with recent time it don;t want to allow post again after few minutes what you say? where is mistake

link table where posting data save

msn_users table

where user information save

structure of my Tables

Right, this was getting way too confusing, so I’ve created a set of tables in a test DB that match yours (as far as I can tell) and discovered that my query wasn’t doing what I thought. I’ve rewritten and tested the whole thing, so give this a go and let me know how you get on:


$query = "SELECT link.dtime as last_post, CONCAT( DATE( NOW() ), ' ', TIME(msn_users.time) ) as period_end
FROM link, msn_users
WHERE link.userid = msn_users.id
AND msn_users.id = $id
ORDER BY link.dtime DESC
LIMIT 1";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$canPost = TRUE;

if (mysql_num_rows($result))
{
    $row = mysql_fetch_assoc($result);

    $period_end = new DateTime($row['period_end']);
    $last_post = new DateTime($row['last_post']);
    $now = new DateTime();

    if ($period_end > $now)
    {
        $period_start = clone $period_end;
        $period_start->sub(new DateInterval('P1D'));

        if ($last_post > $period_start) {
            $canPost = FALSE;
        }
    }
    elseif ($last_post > $period_end)
    {
        $canPost = FALSE;
    }
}

if ($canPost)
{
    echo "You can post!";
}
else
{
    if ($period_end < $now) {
        $period_end->add(new DateInterval('P1D'));
    }

    $interval = $now->diff($period_end);

    echo 'Sorry, you can only post again in: ' . $interval->format('%H:%I:%S') . ' hours';
}

Note that I’ve used the mysql functions in this example only because I was too lazy to rewrite using mysqli or PDO - but you should do this yourself before you put this code into production!

There’s probably a neater way to write this code, but it’s been a long day… maybe someone with fresh eyes will have some suggestions on how to improve it. :slight_smile:

i think its working fine Thanks :slight_smile:
but here one more thing which i required i want to bound users to post under 24 hours otherwise they can’t post
and for this situation whenever user Login i am showing them a count down timer which show them remaining time in which they have to post otherwise they will not able to post if they are unabel to post in 24 hours
so for this purpose i need times in second which i can give in my count down timer.
But this 24 hours count after completing their last post 24 hours

This section of code gives you everything you need to know to do that:


if ($period_end < $now) {   
    $period_end->add(new DateInterval('P1D'));   
}   

$interval = $now->diff($period_end);   

echo 'Sorry, you can only post again in: ' . $interval->format('%H:%I:%S') . ' hours';  

You need to calculate the interval between the current time, and the end of the current 24 hour period, and then output that as seconds. The manual page on the DateInterval object will help you get the hours, minutes and seconds values so you can calculate the total number of seconds remaining for your countdown timer.

honestly i never used classes that’s why don’t have much understanding of the working of this.
can you little explain so i can do accordingly . :slight_smile:

Well, see here in the code where we create our DateTime objects:


$period_end = new DateTime($row['period_end']);
$last_post = new DateTime($row['last_post']);
$now = new DateTime();

These simple objects just represent the three points in time that we need to know… $now is current time, $last_post is the time of the last posting made by the user, and $period_end is the hour of the current day when one 24 hour period ends and another begins.

$period_end can occur either before or after the current time, so we need to check that, and if it’s in the past then we need to add 24 hours to to get the time when the current period will end:


if ($period_end < $now) {
    $period_end->add(new DateInterval('P1D'));
}

You can find details in the PHP manual, but basically P1D means “period of 1 day”, which is the amount of time we’re adding to our $period_end object.

We can then find the amount of time left between now and the end of the current period by calling the diff method:


$interval = $now->diff($period_end);

This returns a DateInterval object.

You can then ask this object for the number of days/hours/seconds etc. between the two times:


echo $interval->h; // Number of hours
echo $interval->s; // Number of seconds

You can use these examples to get all the data you need to display your countdown timer.

but this will tell me remaining minutes if user has already post for completing his 24 hours period. but i want that when user 24 hours start for post and i want to show people that you have — time remaining for post otherwise if he don’t post under 24 hours i can restrict him for next post. that’s what i need actual .

You just need to move things around a little - you’ve got all the pieces of the puzzle, you just need to put them together.


// Move interval calculating code here

if ($canPost) 
{ 
    // Your code here warning the user they need to post within the next x seconds 
} 
else 
{ 
    echo 'Sorry, you can only post again in: ' . $interval->format('%H:%I:%S') . ' hours';  
} 

sorry don’t get what i move ?
and did i copy or move all code to top side ?
sorry for disturbing you again and again and taking your time but can you setup this how you are saying exactly ?

because if i move the same code on the top his execution will not change mean that give the remaining time for next post but i need something else

I was trying to point you in the right direction rather than just writing the code for you, as it doesn’t help you at all if you can’t maintain the code yourself afterwards. But if you’re really stuck, here’s what you need to do:


// We move this code here because it's now use for both outcomes of the IF condition below
if ($period_end < $now) {
    $period_end->add(new DateInterval('P1D'));
}

$interval = $now->diff($period_end);

if ($canPost)
{
    $seconds = ($interval->h * 3600) + ($interval->m) * 60 + $interval->s;
    echo 'You have: ' . $seconds . ' seconds left to post in the current period';
}
else
{
    echo 'Sorry, you can only post again in: ' . $interval->format('%H:%I:%S') . ' hours';
}