kev wood Posted June 3, 2008 Share Posted June 3, 2008 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 More sharing options...
kbh43dz_u Posted June 3, 2008 Share Posted June 3, 2008 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. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556374 Share on other sites More sharing options...
DarkWater Posted June 3, 2008 Share Posted June 3, 2008 Use cron. Apache kills HTTP requests after 300 seconds and PHP timesout after 30 seconds (defaults). Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556376 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 i have never used the cron function where can i find some good tutorials on this. thanks for the replies. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556386 Share on other sites More sharing options...
kbh43dz_u Posted June 3, 2008 Share Posted June 3, 2008 http://www.tech-geeks.org/contrib/mdrone/cron&crontab-howto.htm (all you need) Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556392 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 thank you Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556393 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 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. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556445 Share on other sites More sharing options...
BlueSkyIS Posted June 3, 2008 Share Posted June 3, 2008 cron allows you to run as often as every minute. unless your host has done something funky to cron, you might want to take another look at the manual. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556460 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 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. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556500 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 is there away of using a mysql db to store the time of the last message then add 23:59 to this time and get php to evaluate the two times and if the time has passed then let the user have access to the rest of the site? Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556513 Share on other sites More sharing options...
Prismatic Posted June 3, 2008 Share Posted June 3, 2008 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 } ?> Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556523 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 thanks for the reply. you didnt have to post the code i dont mind working it out for myself. you were good enough to come up with the idea in the first place. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556528 Share on other sites More sharing options...
freeloader Posted June 3, 2008 Share Posted June 3, 2008 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 = '' Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556530 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 the now() function is only storing the year in the db is another that will store the time aswel Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556582 Share on other sites More sharing options...
freeloader Posted June 3, 2008 Share Posted June 3, 2008 Now will display date + time there. Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556592 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 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! Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556606 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 no it is defiantly just storing the the year 2008. what data type should the table be set up with to hold the date? i mean by this varchar, int, blob etc Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556613 Share on other sites More sharing options...
kev wood Posted June 3, 2008 Author Share Posted June 3, 2008 i have had a look around and from what i can gather you can store the date and time in a sting or integer data type so it couldnt be the way i have set my table up as it is done with the int data type. Or am i wrong? Link to comment https://forums.phpfreaks.com/topic/108512-time-delay-on-script-execution/#findComment-556635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.