如何在没有Crontab的情况下每两周在PHP中调用一个方法
I want to update the div in html to display different contents every week with just php and without the use of cron. How will it be posssible to achieve it ? The following code is for activating the code every 2 month on the 10nth day but I guess the logic will be different. If possible some hint or example will be great ! I will love to hear from you!
// check if current day is 10th and month is an even number
if (date('d')==10 && date('m') % 2 == 0) {
// get todays-date (format: yyyymmdd)
$today = date('Ymd');
// get info about last run
$last_run = variable_get('my_last_run', 0);
// check last run was not today
if ($last_run!=$today) {
// set last run to today
variable_set('my_last_run', $today);
/* Place your code here */
}
}
I've just updated your example, now it will work every next Monday:
$weekInYearNumber = (int)date('W');
$weekDayNumber = (int)date('w');
if ($weekDayNumber === 1 && $weekInYearNumber % 2 == 0) {
// Rest of your code.
}
If you can't or don't want to use a cron job for that (which would be the easiest and recommended solution), you can create a PHP script that spends most of the time sleeping, and wakes up to check the date and run whatever you need.
If you don't care about dates and just want to run it every two weeks, you can make it sleep like this:
sleep(14*24*60*60);
Have your script execute your code after that, and put all that inside an infinite loop. Then just run the script from the console so it doesn't have an execution time limit.
Take into account that the script may stop running for different reasons, for example when servers reboot, so you should monitor the process and re-run it automatically if it's not running or on reboot.
This may work for you.
function runEveryWeek(){
sleep(/*for 2 weeks*/);
runEveryWeek();
}
With purpose to display different contents every week, you can use this
$week = (int)date('W');
$odd = ($week % 2) === 0;
if ($ood) {
// First week.
} else {
// Second week.
}
Are you sure ? Your web page will open for 2 month ?
You can apply one logic
Just save next cron date in database. so your logic will like
<?php
if($databaseDate == date()){
//Perform acetion here
// Save your next date here
}
?>
I hope this will solve your problem
If you don't want to do this then you can use PM2
For pm2 you need ssh access of the server.
If you want to do it quickly, you can count weeks since the first of january 2018 (because it was monday: any other year started with a monday will be ok), and then check if that number is odd or even:
$diffBetweenNowAndFirstJan2018 = time() - strtotime("2018-01-01");
$numberOfWeeksSinceFirstJan2018 = floor($diffBetweenNowAndFirstJan2018 / (60 * 60 * 24 * 7));
$isNumberOfWeeksOdd = ($numberOfWeeksSinceFirstJan2018 % 2 == 1)
if($isNumberOfWeeksOdd){
// it's other week time
}
In one line:
if( ( (floor((time() - strtotime("2018-01-01")) / (60 * 60 * 24 * 7))) % 2 == 1) ){
// it's other week time
}
You should not use date('W') as Vladmir says, because between new year's eve you could get two "odd" week consecutively.