[英]How to make so code in script is executed only first day of week when cron job is executing script every hour all days?
I need to send emails every Monday at 08:00 am and I have a PHP script for that.我需要在每周一上午 08:00 发送电子邮件,并且我有一个 PHP 脚本。 Problem is that my web server hosting provider don't allow me to set up cron job whenever I want, instead they have every hour, every day, every week (midnight Sunday) to choose from.
问题是我的网络服务器托管提供商不允许我随时设置 cron 作业,而是他们每小时、每天、每周(周日午夜)都可以选择。
So I thinking I could set the cron job on every hour, and make an if statement that determines if the day is Monday and at the right time.所以我想我可以每小时设置一次 cron 工作,并做出一个 if 语句来确定这一天是否是星期一和正确的时间。
How do you code that if-statement or do you guys have a better solution?你如何编写 if 语句或者你们有更好的解决方案?
I have tried doing sleep("1 day and 8 hours")
but I later read that scripts only run 30 seconds.我试过做
sleep("1 day and 8 hours")
但后来我读到脚本只运行 30 秒。
I don't know how your PHP script works, but you might want to use the following if-statement我不知道你的 PHP 脚本是如何工作的,但你可能想使用以下 if 语句
if ( date("NH") == "108" ) { ... }
This date function returns the week day (1 is Monday) and the time in 24 hours as a 2 digit number string.此日期函数以 2 位数字字符串形式返回星期几(1 是星期一)和 24 小时内的时间。 (See http://php.net/manual/en/function.date.php )
(见http://php.net/manual/en/function.date.php )
N
: ISO-8601 numeric representation of the day of the week,1
(for Monday) through7
(for Sunday)N
: ISO-8601 星期几的数字表示,1
(星期一)到7
(星期日)
H
: 24-hour format of an hour with leading zeros,00
through23
H
:带有前导零的小时的 24 小时格式,00
到23
You can use the following condition in your script will work i think我认为您可以在脚本中使用以下条件
if (date( 'Y-m-d', strtotime( 'monday this week' ) ) == date('Y-m-d') && date('H') == "08") {
// PHP SCRIPT
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.