PHP script to execute at certain times

后端 未结 8 1879
死守一世寂寞
死守一世寂寞 2020-12-03 19:09

Is there a simple way to have a php script execute some html at a certain times of the day?

For example i have on my home page a header and at certain times i want t

8条回答
  •  心在旅途
    2020-12-03 19:48

    The idea of cron and scheudled jobs seems to run counter to what you're actually trying to do. If you want something to display (an iframe in this case) only during certain times, you can simply check the server time during each request, and opt to display it if you're within a given time period.

    Something like this will produce the same effect as a cron job, with more granularity, checking the time at the exact moment the requst is made.

    
    
    = 9 && $hour <= 17) { ?>
      
    
    

    You can expand on the conditional statement to show the iframe multiple times per day, or have your script check whatever external condition you're looking to use to govern the display of your iframe.

    Update: Additional times or types of comparisons could be specified via something like

    = 5  && $hour <= 7)  // 5am - 7am
    ||  ($hour >= 10 && $hour <= 12) // 10am - 12 noon
    ||  ($hour >= 15 && $hour <= 19) // 3pm - 7pm
    ||  ($day == 5)                  // Friday
    ) { ?>
      
    
    

    The idea of periodically adding/removing the iframe from below your header with a server-side cron/task scheduler job is far more complex than simply conditionally displaying it during each request.

    Even if you have some specific task which must run, such as a periodically generated report, the actual job of displaying the results usually don't fall upon the periodic task. The PHP script responsible for showing that iframe would still query the database at the time the request is made for any new content to show, and display it if found, rather than the periodic task somehow modifying the script to include an iframe.

提交回复
热议问题