我可以从代码中设置cron触发时间吗? [英] Can I set cron trigger time from code?

查看:88
本文介绍了我可以从代码中设置cron触发时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在免费托管计划,所以cron选项有限。我希望我的脚本在23:59运行但无法提供语法。他们对我想要的最接近的选择是每十分钟。



我尝试了什么:



在我的代码中,我手动测试如此



I'm on a free hosting plan so the cron options are limited. I want my script to run at 23:59 but can't supply the syntax. The closest option they have to what I want is every ten minutes.

What I have tried:

In my code, I'm manually testing like so

$now = new DateTime("now", new DateTimezone("Africa/Lagos"));<br />
if ($now->format ("H:i") == '23:50')) {}<br />





但它永远不会运行。如果我从地址栏访问此脚本的URL,它将无缝运行。如果我在if条件之外调用file_put_contents(),它就会运行。是否意味着服务器永远不会识别条件中的时间?



But it never ever runs. If I visit this script's URL from the address bar, it runs seamlessly. If I call file_put_contents() outside the if condition, it runs. Does it mean the server never recognizes the time in the condition?

推荐答案

now = new DateTime(now,new DateTimezone(Africa / Lagos)); < br />
if(
now = new DateTime("now", new DateTimezone("Africa/Lagos"));<br /> if (


now-> format(H:i)== '23:50')){}< br />
now->format ("H:i") == '23:50')) {}<br />





但它永远不会运行。如果我从地址栏访问此脚本的URL,它将无缝运行。如果我在if条件之外调用file_put_contents(),它就会运行。是否意味着服务器永远不会识别条件中的时间?



But it never ever runs. If I visit this script's URL from the address bar, it runs seamlessly. If I call file_put_contents() outside the if condition, it runs. Does it mean the server never recognizes the time in the condition?


如果您的cron作业设置选项有限,那么您可以使用cUrl调用脚本(作为CRON的替代)。只需在执行cUrl函数的本地计算机上执行计划任务。
If your cron job setting option is limited then you can call the script with cUrl (as alternative to CRON). Just do a scheduled task on your local machine that executes the cUrl function.


这篇关于我可以从代码中设置cron触发时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆