Jump to content

time delay on script execution


kev wood

Recommended Posts

i am looking at the sleep() function but i have never used this before and would like some pointers if anyone can help.

 

i would like to know if this can be used for what i want it for.  i want to execute a piece of code after a 24 hr wait.  i know that the sleep functions time is done in seconds so i would have to tell it to sleep for 24*60*60.

 

once this time has passed would the code be executed automatically on the server with out no user interaction.

Link to comment
https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/
Share on other sites

Your script will abort before time (24hr) ran out :) But you can save the time to a database and install a cronjob which kicks a script which checks the time every minute (or what you want...). If time has passed 24hr the script can send your code.

this will not work for how i want it to.  i need the counts to be reset after 24 hr has passed from when the first email has been sent out.  the cron function only allows me to schedule the code to be run on a daily basis.  thanks for the replies though i am sure i can find a use for that function very soon.

what i want is say if a user sends a message 09:15 in the morning and this is there last message they are aloud to send that day as there limit has been reached.  i then want all there data about there time of the last message to be deleted 24 hr as passed.  so at 09:15 the next morning they can once again send messages.

 

correct me if i am wrong but the cron function would only allow me to run any code once a minute, hour, day or week etc. as the time of the last message could be sent at anytime of the day then the cron function would only run at the time i have set it up to run for.  as i would need the code running exactly 24 hours after the last message has been sent this would not work.

When a user sends a new message record the current php time() timestamp into the database

 

<?php

# $last_message is the timestamp you recorded when they sent their last message
$time_dif = time() - $last_message;

$time_dif = intval($time_dif / 60);

# 1440 is the number of minutes in 24 hours
if($time_dif >= 1440)
{
    # Its been 24 hours
}
else
{
    # It has not been 24 hours
}

?>

You should use mysql for this :)

 

Method 1:

 

Insert the datetime in the mysql DB and next time query it:

 

SELECT * FROM tb_yourtable WHERE (NOW() > Datetimefield + INTERVAL 1 DAY) AND Username = ''

 

Method 2:

 

UPDATE tb_yourtable SET Nextuse = DATE_ADD(NOW(), INTERVAL 1 DAY) WHERE Username = ''

i have made a page which just prints out the contents on the tables and there stored data and it just gave me 2008 in both the columns i had tried to store the date and time in.  i will run it again wit the now() function and see what it gives me this time.

 

strange!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.