Results 1 to 2 of 2

Thread: Check timestamp is over 5 minutes ago?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Check timestamp is over 5 minutes ago?

    Hi guys, does anybody know a way of only inserting the same thing into the database if they are 5 minutes apart? For example :

    Jamie Warren listened to Capital FM - goes in at 12:06pm

    but if i then if i refresh the page it adds the record again but with a different time stamp, is there a way to only add the same record if the timestamps are over 5 minutes apart?


    $time = time();
    $sql_insert1 = "INSERT into `activity`
    (`username`,`activity`,`time`,`type`
    )
    VALUES
    ('$id','$text','$time','broadcast'

    )
    ";


    mysql_query($sql_insert1) or die("Insertion Failed:" . mysql_error());

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Check timestamp is over 5 minutes ago?

    Just query the table and compare the current time against your 'time' field.

    Use MySQL's DateDiff function and if the result is less than 5 minutes, then don't bother saving.

    You could also speed this up by caching the last save for a particular user in memory on the server and then, only if that cache is empty, do the query to check the date in the database.

    If it passes, then save, otherwise just ignore it.

    I would also like to point out that your SQL is potentially full of SQL Injection. I would recommend using parameterized queries when doing any SQL work.

    One of my favourites for PHP is PDO.

Posting Permissions

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



Click Here to Expand Forum to Full Width