Vi presento questo problema che per me è un mistero.

ogni 24 ore devo sorteggiare 5 (in un caso 4) link per categoria da visualizzare. Questi link ogni 24 ore devono essere risorteggiati.

Questo è il codice che ho scritto:

Codice PHP:
// Controllo che il random avvenga una volta ogni 24 ore

        //$flagEsegui = false;
        
$text=fopen("random24h.txt","rw");
        
$lastday fgets($text,255); //valore preso dal file
        
        
fclose($text);
        
        
//$arrday = getdate();
        //$today = $arrday['mday']; //valore del giorno corrente

        
if((time()- $lastday) > 86400){
        
            
//Eseguo le nuove estrazioni
                    
                    //Azzero tutti i campi estratto
                    
$query "UPDATE link SET estratto = false";
                    
$result mysql_query($query);
                    
                    
// LINK DELLA CATEGORIA 1 LUI LEI
                    
$query "SELECT * FROM link WHERE pos_cat=1 ORDER BY RAND() LIMIT 5";
                    
$result mysql_query($query);
                                        
                    while (
$row mysql_fetch_array($result)){                    
                        
$query2 "UPDATE link SET estratto = true WHERE id= '$row[id]'";
                        
$result2 mysql_query($query2);
                    }
                    
                    
// LINK DELLA CATEGORIA 2 UTENTI
                    
                    
$query "SELECT * FROM link WHERE pos_cat=2 ORDER BY RAND() LIMIT 4";
                    
$result mysql_query($query);
                                        
                    while (
$row mysql_fetch_array($result)){                    
                        
$query2 "UPDATE link SET estratto = true WHERE id= '$row[id]'";
                        
$result2 mysql_query($query2);
                    }
                    
                    
// LINK DELLA CATEGORIA 3 SCUOLA
                    
                    
$query "SELECT * FROM link WHERE pos_cat=3 ORDER BY RAND() LIMIT 5";
                    
$result mysql_query($query);
                                        
                    while (
$row mysql_fetch_array($result)){                    
                        
$query2 "UPDATE link SET estratto = true WHERE id= '$row[id]'";
                        
$result2 mysql_query($query2);
                    }
                    
                    
// LINK DELLA CATEGORIA 4 GENERALE
                    
                    
$query "SELECT * FROM link WHERE pos_cat=4 ORDER BY RAND() LIMIT 5";
                    
$result mysql_query($query);
                                        
                    while (
$row mysql_fetch_array($result)){                    
                        
$query2 "UPDATE link SET estratto = true WHERE id= '$row[id]'";
                        
$result2 mysql_query($query2);
                    }
            
            
// imposto il nuovo valore nel file di testo
            
            
$textfopen("random24h.txt","w"); 
            
fwrite($texttime());
            
            } 
Sembra funzionare bene, però succede una cosa a cui non riesco a trovare spiegazione:
succede che ad un'orario imprecisato invece che estrarre 5 link lo script me ne estrae solo 4!!
Poi dopo un certo periodo di tempo tutto ritorna a posto, con 5 link estratti
Mentre si verifica il problema ho anche controllato nel db: effettivamente solo 4 link hanno settato il campo "estratto" sul valore true.
Inspiegabilmente dopo un po' di tempo tutto torna a funzionare.
A cosa può essere dovuto? Possono influire le impostazioni dell'ora sul server?