How to call a method every 2 weeks in PHP without crontab

I want to update the div in html to show different content every week only with php and without cron. How will this be possible to achieve? The following code is designed to activate the code every 2 months on the 10th day, but I think the logic will be different. If possible, some hint or example would be great! I will be happy 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 */

    }
}
+4
source share
6 answers

I 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.
}
+2
source

cron ( ), PHP script, , .

, :

 sleep(14*24*60*60);

script . script , .

, script , , , , .

0

$week = (int)date('W');
$odd = ($week % 2) === 0;
if ($ood) {
    // First week.
} else {
    // Second week.
}
0

, 2018 ( : , , ), , :

      $diffBetweenNowAndFirstJan2018 = time() - strtotime("2018-01-01");
      $numberOfWeeksSinceFirstJan2018 = floor($diffBetweenNowAndFirstJan2018 / (60 * 60 * 24 * 7));
      $isNumberOfWeeksOdd = ($numberOfWeeksSinceFirstJan2018 % 2 == 1)

      if($isNumberOfWeeksOdd){
            // it other week time
    }

:

     if( ( (floor((time() - strtotime("2018-01-01")) / (60 * 60 * 24 * 7))) % 2 == 1) ){
            // it other week time
    }

( "W" ), , "" .

0

.

  function runEveryWeek(){
     sleep(/*for 2 weeks*/);
     runEveryWeek();
  }
-1

? - 2 ?

cron .

<?php

if($databaseDate == date()){
   //Perform acetion here
   // Save your next date here
}
?>

, .

, PM2

For pm2 you need ssh access to the server.

-1
source

Source: https://habr.com/ru/post/1675872/


All Articles