简体   繁体   中英

setting a php if statement to execute function only once?

I have a table called profile_views on my site that counts how many times a person views a users profile. What i am trying to do is let users see how many visiters they've had to their profile in that week, and every week I on Monday I want the table to be emptied so the count can restart from 0.

I am trying to create a php if statement that says every week on monday carry out the delete mysql query. I have got it to perform only if the day is monday, however I am trying to find a way to execute it only once, preferably at 00:00AM monday morning, because I dont want it deleting results in the table constantly throughout the whole of monday.

<?php
$today=date(l); // Find what today is? using date function

// If today is Monday displays message "Today is Monday" and displays image1.gif
if($today==Tuesday) { // Compare $today with name of the day. 

$result = mysql_query("DELETE FROM ptb_profile_views;") 
or die(mysql_error());

}
?>

您要查找的名称为cron使用它在所需工作日的所需时间调用您的PHP脚本。

you could set up a cron job to automatically execute the script, but a better way (the way I do it) is to just leave the entries in the database and when displaying the number of visitors in the last week, change your query to return only entries within the last week. this might be much easier.

A method without cron jobs :

You can set a variable $ran and store it somewhere (maybe in a file?). Then every Monday, run and store the $ran into the file as true .

If it's any other day, set $ran back to false. This will ensure that the script will only run on Monday, and only run once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM