简体   繁体   中英

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. 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.

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.

How do you code that if-statement or do you guys have a better solution?

I have tried doing sleep("1 day and 8 hours") but I later read that scripts only run 30 seconds.

I don't know how your PHP script works, but you might want to use the following if-statement

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. (See 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)

H : 24-hour format of an hour with leading zeros, 00 through 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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM