1

I am using the code below to show ads to my users. Once any user has clicked on one add, it should not appear to the same user within 24 hours.

$sql2=$Db1->query("SELECT * FROM ads2 WHERE active='1' ORDER BY RAND()");

while($temp=$Db1->fetch_array($sql2)) {

    $sql=$Db1->query("SELECT * FROM ads2_log WHERE user='$username' AND target='{
         $temp['target']}' AND shorten='{$temp['shorten']}' ORDER BY fecha DESC");
    $row=$Db1->fetch_array($sql);
    $showads = true;

    if( $row['fecha'] != "" ){

        $hourdiff = round(($row['fecha'] - time())/3600, 1);
        //echo "Last click: ".date("Y/m/d H:i:s", $row['fecha'])." - ".$hourdiff;
        if( $hourdiff < 24 ) { $showads = false; }

    }
    if( $showads == true )
    {

But in my case it's not working for me.

Luka
  • 1,718
  • 3
  • 19
  • 29
Neha
  • 85
  • 1
  • 1
  • 8
  • 3
    Try to debug your code and update your question with debug results. [Also](http://stackoverflow.com/a/60496/2898694). And also, use correct name for the topic: when I clicked it, I had thought, that you need to calculate page loading time or smth like this. – Sharikov Vladislav Aug 06 '14 at 15:41
  • 2
    In what way specifically is it not working? What _is_ happening? – Patrick Q Aug 06 '14 at 15:42
  • @SharikovVladislav , I have changed the topic name -- – Neha Aug 07 '14 at 13:52
  • @PatrickQ : the ads just diappear after first successfull click. they should come back after 24hr . but that particular add is not appearing for the user who clicked it. – Neha Aug 07 '14 at 13:53

1 Answers1

0

Let's suppose that the database column referred to by fecha contains a DATETIME character sequence in the following form (it's used by MySQL): YYYY-MM-DD hh:mm:ss. In that case you can calculate the time difference between the current time and the time stored in the database in the following way:

$hourdiff = round( (time()-strtotime($row['fecha']))/3600, 1);

PHP time() function returns a number of milliseconds since the start of the Unix epoch (January 1 1970 00:00:00 UTC), so you need to use strtotime to convert the date string from the database to a number of milliseconds. After that, you're ready to calculate the difference.

Luka
  • 1,718
  • 3
  • 19
  • 29
  • ok, i will check this and will write update here .. for more suggestions – Neha Aug 07 '14 at 19:14
  • I have one more suggestion. I.e Ads shows reappears again at 00.00 hrs on server time. When they are clicked. they should disappear and appear again at 00.00 .. thanks for help – Neha Aug 08 '14 at 09:39