0

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 :)

MyiWorld
  • 51
  • 1
  • 8
  • 2
    I would strongly suggest a cron job. I would also strongly suggest top stop using `mysql_*` functions and use PDO or mysqli. – Brad Mar 25 '14 at 21:31
  • ^ Exactly, you can set up an @daily cron job. Now it runs at midnight every night already, no need to perform checks in php as well. – skrilled Mar 25 '14 at 21:35
  • Cronjob is what you are looking for. – Aziz Saleh Mar 25 '14 at 21:35
  • Using [MySQL Events](https://dev.mysql.com/doc/refman/5.6/en/events.html) could be an option for you, without any need for PHP – Mark Baker Mar 25 '14 at 21:41
  • hi all, thanks for your fast response, i am only a 15 year old coder so haven't really expanded out into languages other than the basics yet. nevertheless i will try using `cronjob` and feedback how it works. Also if you don't mind me asking, what is mysqli? – MyiWorld Mar 25 '14 at 21:44
  • mysqli is an "improved" mysql library, that has fever (known) security holes. But foremost mysql_* lib is deprecated and will eventually stop working! mysqli also supports object oriented programming in a better way then mysql – Philip G Mar 25 '14 at 21:51

3 Answers3

1

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.

Community
  • 1
  • 1
prk
  • 3,781
  • 6
  • 17
  • 27
  • Hi, thanks for your fast reponse. also thankyou for the links, i believe i have a small understanding of cronjobs now. however how would i make it execute at 00:00:00? – MyiWorld Mar 25 '14 at 21:48
1

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.

fejese
  • 4,601
  • 4
  • 29
  • 36
1

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
   }
?>
Philip G
  • 4,098
  • 2
  • 22
  • 41