SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 43 of 43
  1. #26
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here are the both queries what i am using

    PHP Code:
      $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($result0'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 (
    == mysql_result($exe20'total')){  
       echo 
    "you are allow";  


  2. #27
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    I thought that maybe you were planning to do something like this:

    PHP Code:
    $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 (
    == mysql_result($exe20'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($result0'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';


  3. #28
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #29
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #30
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    link table where posting data save

    6PSOFfs.png

    msn_users table

    where user information save
    rQWK6eW.png

    structure of my Tables

  6. #31
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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:
    PHP Code:
    $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.

  7. #32
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think its working fine Thanks
    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

  8. #33
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by realcoder View Post
    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:
    PHP Code:
    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.

  9. #34
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 .

  10. #35
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Well, see here in the code where we create our DateTime objects:
    PHP Code:
    $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:
    PHP Code:
    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:
    PHP Code:
    $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:
    PHP Code:
    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.

  11. #36
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 .

  12. #37
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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.
    PHP Code:
    // 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';  


  13. #38
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ?

  14. #39
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  15. #40
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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:

    PHP Code:
    // 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->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';  


  16. #41
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hii
    something is wrong in code here i put condition that if 24 hours end up and user don't post he can't access post page and i apply exit condition. but i found that after completing 24 hours without posting next 24 hours count down time automatic start instead of doing exit what i want to do please see below my code

    Code:
    $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 ($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'; 
        if($seconds <= 0)
        {
            echo "Time Over";
            exit;
        }
    
    }  
    else  
    {  
        echo 'Sorry, you can only post again in: ' . $interval->format('%H:%I:%S') . ' hours';   
    }
    Please let me know where is the mistake how can i restrict people if they dont post withing last 24 hours time period they can't post next. Thanks

  17. #42
    SitePoint Addict
    Join Date
    Oct 2010
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bro don't you suggest any solution in.above scenario..

  18. #43
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,401
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi realcoder,

    Sorry for taking a while to get back to you - I think the following changes should do what you want:
    PHP Code:
    if ($canPost)  
    {  
        
    //Check if user posted in the previous period
        
    $last_post_interval $period_end->diff($last_post);
        if (
    $last_post_interval->>= 2)
        {
            echo 
    'Time over - you failed to post in the last 24 hour period';
        }
        else 
        {
            
    $seconds = ($interval->3600) + ($interval->m) * 60 $interval->s
            echo 
    'You have: ' $seconds ' seconds left to post in the current period'
        }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •