简体   繁体   English

如何使脚本中的代码仅在一周的第一天执行,因为 cron 作业每天每小时都在执行脚本?

[英]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) through 7 (for Sunday) N : ISO-8601 星期几的数字表示, 1 (星期一)到7 (星期日)

H : 24-hour format of an hour with leading zeros, 00 through 23 H :带有前导零的小时的 24 小时格式, 0023

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.

相关问题 每秒使用cron作业执行脚本的替代方法? - Alternatives to executing a script using cron job every second? cron作业未执行php脚本 - cron job not executing the php script PHP脚本通过cron作业执行时的不同行为 - Different behavior when PHP script executed through cron job 如何在Cpanel每天6点钟完成cron工作 - how to make cron job every day at 6 O'clock by Cpanel (PHP)当脚本作为cron作业执行时,如何获取文件名(或文件路径)? - (PHP) How to obtain the filename (or file path) when a script is executed as a cron job? Cron Job-如何在特定日期两次执行脚本? - Cron Job - How to execute a script between two times on specific days? php 脚本在一天、一周、2 周后通过 cron 作业向注册用户发送电子邮件 - php script to send emails by cron job after a day , week ,2 weeks to the registred users 您如何使用cron作业使HTML页面内的PHP脚本每分钟重复一次? - How do you use a cron job to make a PHP script inside an HTML page repeat every minute? 除了第一个小时“0”的第一分钟“0”外,每2分钟运行一次cron作业 - Run cron job every 2 minutes except the first minute “0” of the first hour “0” Cron Job-为什么我的php脚本不仅选择一天(我希望它能正常工作),所以现在还选择了7天 - Cron Job - why does my php script select not only one day (as I want it to work) which is 7 days forwads from right now but it selects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM