0

I am using this code

$q = mysql_query("SELECT `time` from `table`");
$row = mysql_fetch_assoc($q);
$timeDiff = time() - $row['time'];
if ($timeDiff >= 86400){
  //run code
}

but I am getting an error in the second line can someone please help me with this.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
stylish
  • 3
  • 4

3 Answers3

0

Here you go:

$datetime = new DateTime("+1 days");
$date = $datetime->format("Y-m-d H:i:s");

$timeDiff = time() - $row['time'];

if($timeDiff >= '$date) {

 //Code Here

}

Edit:

You can update your code in your_file.php and run this:

0 0 1 * * php /var/www/vhosts/your_somain.com/httpdocs/scripts/your_file.php

This will run once a month, on the first day of the month at midnight (i.e. January 1st 12:00am, February 1st 12:00am etc.):

For further explanation:

Reference: tutsplus.com

Hope it will help you.

Noman
  • 1,459
  • 2
  • 18
  • 38
  • what if i want to check it monthly how will i do that? – stylish Sep 07 '16 at 11:41
  • Simply you can add this line `$datetime = new DateTime("+1 month");` – Noman Sep 07 '16 at 11:42
  • actually i am making a salary management system and i want the salary to be updates every month will this piece of code work with that? – stylish Sep 07 '16 at 11:49
  • Yes, it will work but it is not a good approach. Use a CRON job to handle this. http://en.wikipedia.org/wiki/Cron. Please up vote and accept the answer if it solves your purpose. Thanks! – Noman Sep 07 '16 at 11:51
  • but when ever the code will run the month will be updates automatically right? – stylish Sep 07 '16 at 11:53
  • but i want the salary to be updated on 1st of every month not when ever the code runs – stylish Sep 07 '16 at 11:57
  • When you set the date to `+1 month` and then you apply your condition so it will updated on 1st of every month. – Noman Sep 07 '16 at 11:58
  • but there is a problem when ever the page is loaded on the first of the month it will keep updating it so the salary will be updates for than once that why i used the code which i gave up cant that work with – stylish Sep 07 '16 at 12:05
  • That why I said to you that kindly use CRON job for this purpose. Wait I will give you a small example. – Noman Sep 07 '16 at 12:07
  • i have no idea about the corn can u explain me how to do that plzzz – stylish Sep 07 '16 at 12:08
  • how should i run the line u mentioned – stylish Sep 07 '16 at 12:27
  • i have on another question what if the system is off will it still run?and the salary will be updated? – stylish Sep 07 '16 at 14:51
  • You uploaded it on server ? – Noman Sep 07 '16 at 17:50
0

output error

 $q = mysql_query("SELECT `time` from` table`");

    if (!$q) {
       $message = 'Invalid query:'.mysql_error(). "\n";
       die($message);
    }

    while($row = mysql_fetch_assoc($q)) {
        $timeDiff = time() - $row['time'];

        if ($timeDiff >= 86400){
           //run code
        }
    }
YaHui Jiang
  • 130
  • 6
0

The problem is not the second line. The problem is the first line. "SELECT time fromtable" - table is a reserved keyword in mysql.

Michael
  • 405
  • 4
  • 10