How to run a specific condition in script after a specific time in php -
i have 2 ad's , want run them in difference of 8 hours, means ad1
start when script start , ad2
start after 8 hour specified 28800
seconds. seeking functions sleep()
, time_sleep_until()
, bit confused how use them between ad's.
my ad's defined in array. also, tried once sleep
function in localhost execute 1 ad , 1 after sleep(28800)
, , script continues executing , displays output of both ad's. might small problem , didn't apply logic properly.
it sounds there confusion on php responsible doing here. when makes request web server, php script being invoked, after serving request, done , exits. when next request comes in, has no knowledge of first request.
if use sleep(n)
function sleep n seconds, pause response of web server, bad idea , not trying achieve.
generally speaking, if want switch content based on time, better work off of server time (which known) rather "how long server has been running" has no meaning in real world.
in simple example, let's want serve 3 different ads based on 8-hour blocks. might write code finds hour of day, , selects ad appropriately. example:
$selection = (date('g',time()) / 8); switch ($selection) { case 0: echo 'ad option 1!'; break; case 1: echo 'ad option 2!'; break; case 2: echo 'ad option 3!'; break; }
the date('g',time())
function returns hour portion of current system time 0-23. dividing 8 give either 0, 1, or 2, , there can select display.
not sure satisfy trying do. if not, may need involve kind of database, , have kind of job updates ad serve @ interval (i'd avoid if possible).
Comments
Post a Comment