Domanda

So I have a piece of code which is supposed to reset a message of the date (motd) at midnight, my aim is that once the time reached midnight (00:00:00 GMT) that it resets the message of the day back to its original state. So far i have tried the following:

if (gmdate('H:i:s') === '00:00:00') { mysql_query(//execute code) }

all suggestions are appreciated. Thank you.

Edit: forgot to mention my website is hosted from a webhost called nitrousnetworks not localhost. will this affect anything?

Edit-2: found a cron job section in cPanel X. i've set it to run every 24h at midnight but what do i put in the Command: section?

Edit-3: Thankyou all, i believe i have got it now :)

È stato utile?

Soluzione

Well I'd suggest you use an cronjob/script for this.

On some hosts they have a function available for you to just set up a cronjob easily. Otherwise you could check up how to do it yourself - if needed.

How to write a Cron Job to execute simple php script? How to create cron job using PHP?

Edit; Also like someone commented, use mysqli rather than mysql.

Altri suggerimenti

Just so you have an alternative of the strongly suggested cron triggered php scrip, here's an other solution: you can set up a MySQL scheduled event

CREATE EVENT reset_motd
ON SCHEDULE AT '2014-03-25 00:00:00' + INTERVAL 1 DAY
DO UPDATE ...

Of course, this is only a solution if you have all you inputs in the database available for the defined event.

the best solution for this kind of task is probably a Cron Job

The problem with your solution is that it depends on a user visiting your site exactly 00:00:00. Witch is a quite bad design.

One other solution would be to have it reset the message when the first user after midnight is visiting your site.

One example would be Like this:

<?php
   $today = new DateTime("today");
   $result = $mysqli->query("SELECT * FROM firstVisit WHERE date='" . $today->format("Ymd") "'");

   if($result->num_rows == 0){
        $mysqli->query("INSERT INTO firstVisit (`date`) VALUES('" . $today->format("Ymd") . "')");

        //Do your update
   }
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top